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
.
name | type | description |
---|---|---|
keycode | string | number | Array.<number> | Array.<any> |
The Key Codes to enable capture for, preventing them reaching the browser. |