Numpad Input

Listen for specific keydown events from the numeric keypad (numpad) to trigger actions in your game.

Numpad Input, running
1024 × 768
GitHub
class Example extends Phaser.Scene
{
    preload ()
    {
        this.load.setBaseURL('https://cdn.phaserfiles.com/v410');
        this.load.spritesheet('bobs', 'assets/sprites/bobs-by-cleathley.png', { frameWidth: 32, frameHeight: 32 });
    }

    create ()
    {
        this.input.keyboard.on('keydown-NUMPAD_ZERO', function ()
        {
            this.add.image(0, 0, 'bobs', 0).setRandomPosition();
        }, this);

        this.input.keyboard.on('keydown-NUMPAD_ONE', function ()
        {
            this.add.image(0, 0, 'bobs', 8).setRandomPosition();
        }, this);

        this.input.keyboard.on('keydown-NUMPAD_TWO', function ()
        {
            this.add.image(0, 0, 'bobs', 16).setRandomPosition();
        }, this);

        this.input.keyboard.on('keydown-NUMPAD_THREE', function ()
        {
            this.add.image(0, 0, 'bobs', 24).setRandomPosition();
        }, this);

        this.input.keyboard.on('keydown-NUMPAD_FOUR', function ()
        {
            this.add.image(0, 0, 'bobs', 32).setRandomPosition();
        }, this);

        this.input.keyboard.on('keydown-NUMPAD_FIVE', function ()
        {
            this.add.image(0, 0, 'bobs', 48).setRandomPosition();
        }, this);

        this.input.keyboard.on('keydown-NUMPAD_SIX', function ()
        {
            this.add.image(0, 0, 'bobs', 80).setRandomPosition();
        }, this);

        this.input.keyboard.on('keydown-NUMPAD_SEVEN', function ()
        {
            this.add.image(0, 0, 'bobs', 52).setRandomPosition();
        }, this);

        this.input.keyboard.on('keydown-NUMPAD_EIGHT', function ()
        {
            this.add.image(0, 0, 'bobs', 60).setRandomPosition();
        }, this);

        this.input.keyboard.on('keydown-NUMPAD_NINE', function ()
        {
            this.add.image(0, 0, 'bobs', 68).setRandomPosition();
        }, this);

        this.add.text(10, 10, 'Press any numpad button', {fontSize: '20px'});
    }
}

const config = {
    scale: { mode: Phaser.Scale.FIT },
    type: Phaser.AUTO,
    parent: 'game-container',
    pixelArt: true,
    scene: Example
};

const game = new Phaser.Game(config);

More in Keyboard22 examples