Focus management
KeyboardManager
The KeyboardManager
class is responsible for handling keyboard inputs in your game. It’s part of
a system that includes focus management and input handling.
It tracks the state of the keyboard and the application. The _downKeys
set keeps track of which
keys are currently being pressed.
The KeyboardManager
registers event listeners for various keyboard and mouse events. It also connects several methods
to signals, which are emitted when certain events occur.
The onKeyDown
and onKeyUp
methods handle the pressing and releasing of keys, respectively. They update the _downKeys
set and perform actions based on the keys pressed.
The addKeyBinding
method allows your game to specify what action should be taken when a certain key is pressed. It
takes a direction (which could be an actual direction like ‘up’ or ‘down’, or ‘Enter’ for the enter key), a key code, and optional modifiers (like whether the alt, shift, or ctrl keys should be held down).
The removeKeyBinding
and removeAllKeyBindings
methods allow the application to remove these bindings.
The isKeyDown
method checks if a certain key or set of keys is currently being pressed.
The onDirectionPressed
method is called when a direction key is pressed. It steps through the current KeyboardMap
in the given direction.
KeyboardFocusManager
The KeyboardFocusManager
class is responsible for displaying on-screen indicators for focused elements. It listens
for changes in focus and updates the display accordingly. It’s initialized with a type for rendering the focus indicator.
By default, this is the DefaultKeyboardFocusManagerSprite
class, which renders a simple rectangle around the focused element.
The KeyboardFocusManager
pools instances of the focus indicator class to avoid creating and destroying them constantly.
You can create your own focus indicator class by extending the KeyboardFocusManagerSprite
class and overriding the
draw
method, or by implementing the IKeyboardFocus
interface:
You can also tweak the DefaultKeyboardFocusManagerSprite
class by just changing it’s static properties:
Like so:
Focusable Elements
Think of KeyboardMap
as a layer of focusable elements. Each element has a focusable
property, which is a boolean
that determines whether the element can be focused on. Anything in your game can be a focusable element if it has a
focusable
property — but for more granular control, make a custom class that extends Container, and implement the
IFocusable
interface.
KeyboardMap
KeyboardMap
s are used to manage keyboard focus for interactive elements in your game. It has several properties to
track the state of the keyboard and the application, and holds a list of focusable elements and their neighbours. It
also serves double-duty as a focus trap, preventing the user from navigating outside of the focusable elements.
The pushMapLayer
and popMapLayer
methods are used to manage layers of KeyboardMap
objects. When a new layer is pushed, it becomes the active layer and the previous layer is deactivated. When a layer is popped, it is removed and the next layer down becomes active.
Popups
When a popup is shown, it automatically adds its own KeyboardMap
layer, and removes the layer when it’s hidden.
This allows the popup to handle keyboard inputs without interfering with the rest of the application.
Examples
To see examples of this in action, check out the Button Example, the Focusables Example, or the Popups Example on the dill pixel website.