[SOLVED] Pressing spacebar to select menu item

I’m making a menu system for my game’s title screen and I want to be able to select a menu item using the spacebar. I can currently do it using the enter key, but I would prefer to be able to do it using the spacebar as well. This is the code I’m using for the menu navigation:

this.input.keyboard.on('keydown', event => {
        switch (event.key) {
            case 'ArrowUp':
                activeText -= 1;
                this.events.emit('CHANGE_BUTTON');
                break;
            case 'ArrowDown':
                activeText += 1;
                this.events.emit('CHANGE_BUTTON');
                break;
            case 'Enter':
                this.events.emit('SELECT');
                break;
//this particular instance doesn't work:
            case 'SpaceBar':
                    this.events.emit('SELECT');
                break;
        }
    });

I’ve been looking around the documentation, but I can’t seem to find any examples where spacebar isn’t added separately (and even when I tried to add it in that way, it wasn’t recognised)

Any help would be appreciated :slight_smile:

Hello again :slight_smile:.
https://keycode.info/

So I believe it just should be:
case ' ':

2 Likes

Whoa. That works! …but that’s also grossly inconsistent with the other keycodes :laughing:
Oh JavaScript

As an alternative that is more consistent you could use

event.code

instead of

event.key

Then you could use Space. Did not test it.