Class: KeyboardManager

Phaser.Input.Keyboard. KeyboardManager

The Keyboard Manager is a helper class that belongs to the global Input Manager.

Its role is to listen for native DOM Keyboard Events and then store them for further processing by the Keyboard Plugin.

You do not need to create this class directly, the Input Manager will create an instance of it automatically if keyboard input has been enabled in the Game Config.


new KeyboardManager(inputManager)

Parameters:
Name Type Description
inputManager Phaser.Input.InputManager

A reference to the Input Manager.

Since: 3.16.0
Source: src/input/keyboard/KeyboardManager.js (Line 14)

Members


captures :Array.<number>

An array of Key Code values that will automatically have preventDefault called on them, as long as the KeyboardManager.preventDefault boolean is set to true.

By default the array is empty.

The key must be non-modified when pressed in order to be captured.

A non-modified key is one that doesn't have a modifier key held down with it. The modifier keys are shift, control, alt and the meta key (Command on a Mac, the Windows Key on Windows). Therefore, if the user presses shift + r, it won't prevent this combination, because of the modifier. However, if the user presses just the r key on its own, it will have its event prevented.

If you wish to stop capturing the keys, for example switching out to a DOM based element, then you can toggle the KeyboardManager.preventDefault boolean at run-time.

If you need more specific control, you can create Key objects and set the flag on each of those instead.

This array can be populated via the Game Config by setting the input.keyboard.capture array, or you can call the addCapture method. See also removeCapture and clearCaptures.

Type:
  • Array.<number>
Since: 3.16.0
Source: src/input/keyboard/KeyboardManager.js (Line 73)

enabled :boolean

A boolean that controls if the Keyboard Manager is enabled or not. Can be toggled on the fly.

Type:
  • boolean
Since: 3.16.0
Default Value:
  • false
Source: src/input/keyboard/KeyboardManager.js (Line 100)

manager :Phaser.Input.InputManager

A reference to the Input Manager.

Type:
Since: 3.16.0
Source: src/input/keyboard/KeyboardManager.js (Line 36)

onKeyDown :function

The Key Down Event handler. This function is sent the native DOM KeyEvent. Initially empty and bound in the startListeners method.

Type:
  • function
Since: 3.16.00
Source: src/input/keyboard/KeyboardManager.js (Line 121)

onKeyUp :function

The Key Up Event handler. This function is sent the native DOM KeyEvent. Initially empty and bound in the startListeners method.

Type:
  • function
Since: 3.16.00
Source: src/input/keyboard/KeyboardManager.js (Line 132)

preventDefault :boolean

A flag that controls if the non-modified keys, matching those stored in the captures array, have preventDefault called on them or not.

A non-modified key is one that doesn't have a modifier key held down with it. The modifier keys are shift, control, alt and the meta key (Command on a Mac, the Windows Key on Windows). Therefore, if the user presses shift + r, it won't prevent this combination, because of the modifier. However, if the user presses just the r key on its own, it will have its event prevented.

If you wish to stop capturing the keys, for example switching out to a DOM based element, then you can toggle this property at run-time.

Type:
  • boolean
Since: 3.16.0
Source: src/input/keyboard/KeyboardManager.js (Line 55)

target :any

The Keyboard Event target, as defined in the Game Config. Typically the window in which the game is rendering, but can be any interactive DOM element.

Type:
  • any
Since: 3.16.0
Source: src/input/keyboard/KeyboardManager.js (Line 111)

Methods


addCapture(keycode)

By default when a key is pressed Phaser will not stop the event from propagating up to the browser. There are some keys this can be annoying for, like the arrow keys or space bar, which make the browser window scroll.

This addCapture method enables consuming keyboard event for specific keys so it doesn't bubble up to the the browser and cause the default browser behavior.

Please note that keyboard captures are global. This means that if you call this method from within a Scene, to say prevent the SPACE BAR from triggering a page scroll, then it will prevent it for any Scene in your game, not just the calling one.

You can pass in a single key code value, or an array of key codes, or a string:

this.input.keyboard.addCapture(62);

An array of key codes:

this.input.keyboard.addCapture([ 62, 63, 64 ]);

Or a string:

this.input.keyboard.addCapture('W,S,A,D');

To use non-alpha numeric keys, use a string, such as 'UP', 'SPACE' or 'LEFT'.

You can also provide an array mixing both strings and key code integers.

If there are active captures after calling this method, the preventDefault property is set to true.

Parameters:
Name Type Description
keycode string | number | Array.<number> | Array.<any>

The Key Codes to enable capture for, preventing them reaching the browser.

Since: 3.16.0
Source: src/input/keyboard/KeyboardManager.js (Line 267)

clearCaptures()

Removes all keyboard captures and sets the preventDefault property to false.

Since: 3.16.0
Source: src/input/keyboard/KeyboardManager.js (Line 402)

destroy()

Destroys this Keyboard Manager instance.

Since: 3.16.0
Source: src/input/keyboard/KeyboardManager.js (Line 415)

removeCapture(keycode)

Removes an existing key capture.

Please note that keyboard captures are global. This means that if you call this method from within a Scene, to remove the capture of a key, then it will remove it for any Scene in your game, not just the calling one.

You can pass in a single key code value, or an array of key codes, or a string:

this.input.keyboard.removeCapture(62);

An array of key codes:

this.input.keyboard.removeCapture([ 62, 63, 64 ]);

Or a string:

this.input.keyboard.removeCapture('W,S,A,D');

To use non-alpha numeric keys, use a string, such as 'UP', 'SPACE' or 'LEFT'.

You can also provide an array mixing both strings and key code integers.

If there are no captures left after calling this method, the preventDefault property is set to false.

Parameters:
Name Type Description
keycode string | number | Array.<number> | Array.<any>

The Key Codes to disable capture for, allowing them reaching the browser again.

Since: 3.16.0
Source: src/input/keyboard/KeyboardManager.js (Line 338)

startListeners()

Starts the Keyboard Event listeners running. This is called automatically and does not need to be manually invoked.

Since: 3.16.0
Source: src/input/keyboard/KeyboardManager.js (Line 175)

stopListeners()

Stops the Key Event listeners. This is called automatically and does not need to be manually invoked.

Since: 3.16.0
Source: src/input/keyboard/KeyboardManager.js (Line 237)