# \[Phaser 3\] Keyboard input problems

**URL:** <https://phaser.discourse.group/t/phaser-3-keyboard-input-problems/270>\
**Category:** Phaser 3\
**Created:** [December 11, 2018, 9:42pm UTC](https://phaser.discourse.group/t/phaser-3-keyboard-input-problems/270 "2018-12-11T21:42:14Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ryba](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/ryba/32/189_2.png) [@Ryba](https://phaser.discourse.group/u/Ryba)\
**Post date:** [December 11, 2018, 9:42pm UTC](https://phaser.discourse.group/t/phaser-3-keyboard-input-problems/270/1 "2018-12-11T21:42:14Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![Telinc1](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/telinc1/32/60_2.png) [@Telinc1](https://phaser.discourse.group/u/Telinc1)\
**Post date:** [December 12, 2018, 7:06am UTC](https://phaser.discourse.group/t/phaser-3-keyboard-input-problems/270/2 "2018-12-12T07:06:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Ryba](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/ryba/32/189_2.png) [@Ryba](https://phaser.discourse.group/u/Ryba)\
**Post date:** [December 12, 2018, 8:43am UTC](https://phaser.discourse.group/t/phaser-3-keyboard-input-problems/270/3 "2018-12-12T08:43:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rich](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/rich/32/4_2.png) [@rich](https://phaser.discourse.group/u/rich)\
**Post date:** [December 12, 2018, 8:49am UTC](https://phaser.discourse.group/t/phaser-3-keyboard-input-problems/270/4 "2018-12-12T08:49:35Z")

</div>

`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)

---

<div class="post-metadata">

**Author:** ![Ryba](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/ryba/32/189_2.png) [@Ryba](https://phaser.discourse.group/u/Ryba)\
**Post date:** [December 12, 2018, 9:45am UTC](https://phaser.discourse.group/t/phaser-3-keyboard-input-problems/270/5 "2018-12-12T09:45:01Z")

</div>

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](https://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

---

<div class="post-metadata">

**Author:** ![Ryba](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/ryba/32/189_2.png) [@Ryba](https://phaser.discourse.group/u/Ryba)\
**Post date:** [December 13, 2018, 11:38am UTC](https://phaser.discourse.group/t/phaser-3-keyboard-input-problems/270/6 "2018-12-13T11:38:04Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![rich](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/rich/32/4_2.png) [@rich](https://phaser.discourse.group/u/rich)\
**Post date:** [December 13, 2018, 2:28pm UTC](https://phaser.discourse.group/t/phaser-3-keyboard-input-problems/270/7 "2018-12-13T14:28:15Z")

</div>

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](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.

---

<div class="post-metadata">

**Author:** ![Ryba](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/ryba/32/189_2.png) [@Ryba](https://phaser.discourse.group/u/Ryba)\
**Post date:** [December 13, 2018, 2:39pm UTC](https://phaser.discourse.group/t/phaser-3-keyboard-input-problems/270/8 "2018-12-13T14:39:02Z")

</div>

Thx for reply! Will look at it 🙂

---

<div class="post-metadata">

**Author:** ![Ryba](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/ryba/32/189_2.png) [@Ryba](https://phaser.discourse.group/u/Ryba)\
**Post date:** [December 13, 2018, 3:02pm UTC](https://phaser.discourse.group/t/phaser-3-keyboard-input-problems/270/9 "2018-12-13T15:02:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rich](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/rich/32/4_2.png) [@rich](https://phaser.discourse.group/u/rich)\
**Post date:** [December 13, 2018, 10:41pm UTC](https://phaser.discourse.group/t/phaser-3-keyboard-input-problems/270/10 "2018-12-13T22:41:12Z")

</div>

Great stuff 🙂

---

<div class="post-metadata">

**Author:** ![kensleyolewis](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/kensleyolewis/32/262_2.png) [@kensleyolewis](https://phaser.discourse.group/u/kensleyolewis)\
**Post date:** [December 21, 2018, 6:48pm UTC](https://phaser.discourse.group/t/phaser-3-keyboard-input-problems/270/11 "2018-12-21T18:48:05Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![rich](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/rich/32/4_2.png) [@rich](https://phaser.discourse.group/u/rich)\
**Post date:** [December 21, 2018, 7:14pm UTC](https://phaser.discourse.group/t/phaser-3-keyboard-input-problems/270/12 "2018-12-21T19:14:22Z")

</div>

Yes, although I did just [publish a version](https://github.com/photonstorm/phaser3-examples/blob/master/public/build/134.js) to the Phaser 3 Examples repo you could pull and use to test with.

---

<div class="post-metadata">

**Author:** ![cflowers](https://avatars.discourse-cdn.com/v4/letter/c/3ec8ea/32.png) [@cflowers](https://phaser.discourse.group/u/cflowers)\
**Post date:** [April 18, 2019, 4:36am UTC](https://phaser.discourse.group/t/phaser-3-keyboard-input-problems/270/13 "2019-04-18T04:36:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ryba](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/ryba/32/189_2.png) [@Ryba](https://phaser.discourse.group/u/Ryba)\
**Post date:** [April 18, 2019, 7:22am UTC](https://phaser.discourse.group/t/phaser-3-keyboard-input-problems/270/14 "2019-04-18T07:22:58Z")

</div>

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.
