Question re: example on P3 text entry

Trying to work with P3 text entry. Using example here: https://phaser.io/examples/v3/view/input/keyboard/text-entry#

When anything is typed in, the letter shows up twice, instead of just once. Obviously, the loop instead of just adding the second character typed to the string is storing the info and adding the entire text back to itself, instead of just concatenating the new character.

create() {
var enteredText = '';
this.input.keyboard.on('keydown', function (event) {
enteredText += event.key;
console.log(enteredText);
}

The same problem happens with the example given (link above): Why, and how to just get the newly entered key to concatenate once to the original string?

It works correctly for me.

Maybe yours is sending extra events? You can add

console.log(event.type, event.key);

Thanks but that doesn’t solve my problem. Why would mine be sending extra events? As I said it’s the same as the example online which also gives me the same error–double keys.
Adding console.log(event.type, event.key); gives me a single response “keydown, a”.

It runs the entire loop twice, but why would it do that? “Create” is not a loop.
This is my entire code:
class GameScene extends Phaser.Scene{

    constructor() {

        super("GameScene");

    }

    preload(){

    }

    create() {
        var enteredText = '';
        this.input.keyboard.on('keydown', function (event) {
               console.log(event.type, event.key);
               console.log("event key " + event.key);
               enteredText += event.key;
               console.log("entered text " + enteredText);    
        });
    }
 }

And it’s giving me 6 console logs! What’s going on?

Maybe you have more than one scene open with this same code ?

Try with ‘keyup’ instead of ‘keydown’

1 Like

The reason it’s sending more than just once is a browser thing (afaik). best solution is checking on keyup, another solution is making a variable that’s true/false, having it set to false when ondown is triggered and making a tween that sets it to true after some time and making an IF statement that does that it only triggers when it’s false/true

Tried in Chrome and Firefox–same result.
Tried keyup, same problem.

Am I really the only one having this problem?
I can’t see a problem with my code at all.
When I press any key, the entire callback function runs twice. Why?

I tested my keyboard on other JS sites, and it works fine (e.g. https://javascript.info/keyboard-events), just not with Phaser. I can’t be the only one.

Try

console.log(event.type, event.key, event.timeStamp);

If timeStamp is identical you can ignore it. There’s a fix for this in Phaser v3.50.0, of that’s the problem.

What do you see in https://w3c.github.io/uievents/tools/key-event-viewer.html?

The time stamp is identical… when is 3.5 going to be out? The current release is only 3.2x


The link you provided for a single key press gives me this.

@samme?

It should come out soon, but idk. You can download and try https://www.jsdelivr.com/package/npm/phaser?version=3.50.0-beta.4&path=dist and see if that fixes it.