[Phaser 3] Keyboard input problems

So I’m developing little side-project game and I stumbled upon something that would seem to be an easy problem to tackle, but I can’t figure it out and it drives me crazy why it’s not working.

So I’ve got my Main Scene in the game and “Second” Scene. In both of theese scenes i’m using same keyboard keys to do some stuff and in both of this scenes I create keycodes for thoose keys just like that:

this.keys.keyEnter = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.ENTER);
this.keys.keyW = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.W);
this.keys.keyS = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.S);

Now the problem is: in my Main Scene I create the “Second” Scene just like that:

let textBoxScene = this.scene.get('TextBoxScene');

I call some functions of “Second” scene (that create some sprites etc.) and whatever I try to do I can’t get inputs in to this “Second” scene… I tried using: this.scene.bringToTop(“Second”) and it doesn’t work…

Might be a silly question, but are you actually starting the “Second” scene? this.scene.get returns the instance of the Scene from the Scene Manager but I don’t think input on it would be registered unless the scene is actually running.

ohh… thx, that might be it! I will check it later and confirm if this was it. It would be funny though, couse when i do “this.scene.get” everything else works in this scene.

this.scene.get will definitely just return a reference to that Scene (as @Telinc1 said), it doesn’t actually start it running. Although, it may already be running (it’s hard to tell from the little code posted above)

Well… this wasn’t it… I prepared example with more of code (i actually copied things that I work on, and cut things that I think don’t matter in this case), mind You that I tried to debug this problem a lot, I googled everything about inputs and scenes, I read scenes guide (from devlogs) and I’m quite upset:

app.js:

import OverworldScene from “./scenes/OverworldScene”;
import MainMenuScene from “./scenes/MainMenuScene”;

const config = {
type: Phaser.AUTO,
width: window.innerWidth,
height: window.innerHeight,
roundpixels: true,
pixelArt: true,
physics: {
default: ‘arcade’,
arcade: {
gravity: { y: 0 }
}
},
scene: [
MainMenuScene,
OverworldScene
]
};

const game = new Phaser.Game(config);

MainMenuScene.js:

import Menu from “…/classes/MenuClass”;
import TextBoxScene from “…/scenes/TextBoxScene”;

class MainMenu extends Menu {

constructor (x, y, scene) {
    super(scene, x, y);
    Menu.call(this, x, y, scene);
};
confirm() {
  
};

};

class MainMenuScene extends Phaser.Scene {
constructor() {
super({
key: ‘MainMenuScene’
});
this.background = null,
this.bottomTextBox = null,
this.keys = {};
}
preload() {

}
create() {
    //this.input.setGlobalTopOnly(false); // <== I tried even this...

    this.keys.keyEnter = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.ENTER);
    this.keys.keyW = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.W);
    this.keys.keyS = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.S);

    this.scene.add('TextBoxScene', new TextBoxScene, true);
    let textBoxScene = this.scene.get('TextBoxScene');
    textBoxScene.events.on('textBoxReady', () => {
        textBoxScene.bottomTextBox.init();
        textBoxScene.bottomTextBox.addText('Tekst, tekst, tekst duuuuuuzoo tekstuuuu i dluuuuuugie wyrazy, ale duuuuuzo tekstu, no tak duzo tekstu, ze niewiemy co z nim zrobic.');
    });
    this.scene.bringToTop('TextBoxScene');

}
update() {

}

};

export default MainMenuScene;

TextBoxScene:

class TextBoxScene extends Phaser.Scene {
constructor(test) {
super({
key: ‘TextBoxScene’,
active: true,
});
this.topTextBox = null,
this.bottomTextBox = null,
this.customTextBox = null,
this.keys = {};
//this.inactive = false;
}
preload() {
}
create() {
this.keys.keyEnter = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.ENTER);
this.keys.keyW = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.W);
this.keys.keyS = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.S);
}
update() {
if (Phaser.Input.Keyboard.JustDown(this.keys.keyEnter)) {
if (this.bottomTextBox.externals.visible) {
this.bottomTextBox.addText(‘a’);
} else if (this.topTextBox.externals.visible) {
this.topTextBox.addText(‘a’);
}
}
if (Phaser.Input.Keyboard.JustDown(this.keys.keyW)) {

  }
  if (Phaser.Input.Keyboard.JustDown(this.keys.keyS)) {
  	     
  } 
}
textBoxBackground(posX, posY, width, height, allocation) {
}

}

export default TextBoxScene;

I’m using the ES6, if all this code isn’t enough for someone with more knowledge than me to debug it I will send whole code to anyone willing to help me. You may notice that I already add TextBoxScene with “this.scene.add” with 3rd argument as true (just like in a guide).

What I noticed is that if I comment mapping keys in create of MainMenuScene (this lines: “this.keys.keyEnter = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.ENTER);”) then I works… so maybe I shouldnt map keys like that? ← this may be a good lead to answering why my controls don’t work

I came to a conclusion that this problem has to do something with this:
this.keys.keyEnter = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.ENTER);
being invoked twice (in the Main Scene and in the TextBox Scene) - action after pressing ENTER works in the Main Scene but doesn’t in the Text Scene. Here’s my guess: maybe you can add one key only once per game, if so how can I use same keys in different Scenes?

Key handling has changed significantly in 3.16, have a read about the changes here: https://github.com/photonstorm/phaser/blob/master/CHANGELOG.md#keyboard-input---new-features

I would suggest making a build from the master branch and just working with that.

2 Likes

Thx for reply! Will look at it :slight_smile:

@rich it works! (with Phaser v3.16.0 Beta 3) thank You very much :slight_smile:

Great stuff :slight_smile:

1 Like

Apologize for asking, but what does it mean to make a build from the master branch? Do you mean process a git pull from Phaser on Github?

Yes, although I did just publish a version to the Phaser 3 Examples repo you could pull and use to test with.

1 Like

Ryba I was wondering what it is that you did to solve this? I am currently stuck on this issue myself.

Well as I recall I did what rich told me to do. I built Beta version of library from hit repository and changed key handling.