Inactive controls memory

I have a text field for commands directly in the game and to be able to type without being bothered by the game controls I’m deactivating them with:

this.input.keyboard.keys[key.keyCode].enabled = false
this.input.keyboard.removeCapture(keyName)

Which works, but when focusing on another computer window or browser tab and returning to the game, the controls are captured again.

I tried to inactivate it again in the window’s focus event, but even running it is still active.

How to ensure that the inactive control remains inactive even after switching windows/tabs?

:wave:

That seems unusual.

Can you check what’s in this.input.keyboard.manager.captures?

print during blur event

image
image

print during focus event

image
image




Basically inactivate all keys (except f12) when clicking on f12 and reactivate them all there by pressing f12 again (to be able to type in the console field)

What actually happens here? You type in the input box and what—?

I’m not sure if I understand your question.

If what you want to know is what I’m trying to do in the code, and what actually happens, is this:

I have game controls on the w, s, a, d keys and I decided to implement a command console directly in the game (even to help in the tests).

I then added the F12 key which displays a text box in the upper left corner (and disappears with it if pressed again).

But typing the letters w, s, a and d not only moved the character but also did not type in the field.

Then I disabled the keys with the code below, which solves

this.input.keyboard.keys[key.keyCode].enabled = false
this.input.keyboard.removeCapture(keyName)

However, when changing the OS window or even the browser tab, it activates the keys again, allowing the character to move when typing in the field (but without blocking the keystroke).

I even tried to use the focus event to inactivate the keys again, but in this case it doesn’t inactivate.

As I showed in the print above, in the blur event he is only with the f12 key active in the blur (as it should be), but when giving the focus he already has all the keys. I’m wondering if it would be possible to forcefully delete the keys from this array

I don’t know exactly what’s going on, but I can tell you:

  • addKey() adds a capture unless you pass enableCapture = false.
  • There’s no need to call addKey() again if a key already exists for that key code.
  • If you already have a Key object you can just do key.enabled = false. You don’t have to look up the key in this.input.keyboard.keys.
  • You can do this.input.keyboard.disableGlobalCapture() and this.input.keyboard.enableGlobalCapture().

Add to your scene:

this.game.events.off(Phaser.Core.Events.BLUR, this.input.keyboard.resetKeys, this.input.keyboard);

Did not work

But on github I was informed that this is a Phaser bug that has already been fixed and will be released in the next version. so just wait.

Thank you very much!

I just tested it and it worked.

It only prevents key.enabled from changing, though, it doesn’t affect captures.