Phaser API Documentation

  Version: 
addCapture(keycode)

Description:

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 events for specific keys, so they don't bubble up the browser and cause the default behaviors.

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 a single key code value:

this.input.keyboard.addCapture(62);

An array of key codes:

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

Or, a comma-delimited 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.

Parameters:

name type description
keycode string | number | Array.<number> | Array.<any>

The Key Codes to enable event capture for.

Returns:
Description:

This KeyboardPlugin object.

Since: 3.16.0