Aedric
Aedric

Reputation: 95

Phaser 3 Examples Text Entry cant detect Z key typed?

This code can't detect the letter "Z" when typed. I got it from Phaser 3 Examples - Text Entry.

var config = {
    type: Phaser.AUTO,
    parent: 'phaser-example',
    width: 800,
    height: 600,
    scene: {
        create: create,
        update: update
    }
};

var keySpace;
var keyBackspace;
var textEntry;

var game = new Phaser.Game(config);

function create ()
{
    this.add.text(10, 10, 'Enter your name:', { font: '32px Courier', fill: '#ffffff' });

    textEntry = this.add.text(10, 50, '', { font: '32px Courier', fill: '#ffff00' });

    // keys = this.input.keyboard.addKeys('A,B,C');

    keySpace = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.SPACE);
    keyBackspace = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.BACKSPACE);

    this.input.keyboard.on('keydown', function (event) {

        if (event.keyCode === 8 && textEntry.text.length > 0)
        {
            textEntry.text = textEntry.text.substr(0, textEntry.text.length - 1);
        }
        else if (event.keyCode === 32 || (event.keyCode >= 48 && event.keyCode < 90))
        {
            textEntry.text += event.key;
        }

        console.log(event);
    });
}

function update ()
{
}

I managed to add this code to do something just fine. I am trying to capture the keyboard typed it and register it on a custom UI. The code won't work if he or she typed "Captain Zulu" which would only register "Captain ulu" because it didn't detect it. Same goes if they typed "Zuzuki" and it would appear "uuki" instead.

 else if (event.keyCode === 38 )
 {
     dosomethingthirtyeight();
     showPanel();
 }
 else if (event.keyCode === 40 )
 {
     dosomethingfortyeight();
     showPanel();
 }

Upvotes: 2

Views: 471

Answers (1)

Aedric
Aedric

Reputation: 95

this fix the problem. I found the keycodes here https://github.com/photonstorm/phaser/blob/v3.20.0/src/input/keyboard/keys/KeyCodes.js#L764

(event.keyCode === 190 || event.keyCode === 90) // the period isnt working too
{
  textEntry.text += event.key;
}

Upvotes: 1

Related Questions