Temporarry disable key captures in game

How temporary disable key captures when i switch to DOM element, for example when user writes text in html text field - game keys must be disabled.
I have tryed this.input.keyboard.disableGlobalCapture() , but it doesn’t work.
this.input.keyboard.clearCaptures() - also not works
When i typing text in textfield - game keys still working.
Full removing captures with removeCapture(keycode) and then creating again i thing bad style of programming.

this.input.keyboard.enabled = false;

For each scene.

2 Likes

Thanks! But if i need keep enabled only key ENTER ? To allow user press ENTER when he wants send text .

You could maybe use addKey() for all the keys and then toggle them with Key#enabled.

But maybe you don’t need ENTER? You could listen to the input’s change event instead.

Thanks, that works fine :

keySPACE = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.SPACE);

 keySPACE.enabled = false;

For anyone who finds this thread in a search (like me) while trying to enable key captures for DOM elements outside of your Phaser game, you can use this.input.keyboard.enableGlobalCapture() (Keyboard plugin docs).

This does not disable Phaser from capturing the keyboard, so if you want to disable input in Phaser while enabling input elsewhere, you can do something like:

this.input.keyboard.enabled = false;
this.input.keyboard.enableGlobalCapture();

and this.input.keyboard.disableGlobalCapture(); to re-disable global capture.

Apologies if this post necromancy bothers anyone, just wanted to add this here for anyone else who finds this via Google search :]

5 Likes
this.input.keyboard.manager.enabled = false;

will also prevent key event captures.