Phaser: Keyboard events

This post is part of a Phaser series. Click here to see the first post of the series.

Mouse events are fired on a GameObject, because with the mouse we click on elements.

With the keyboard instead we press keys, not linked to any GameObjec…


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com

This post is part of a Phaser series. Click here to see the first post of the series.

Mouse events are fired on a GameObject, because with the mouse we click on elements.

With the keyboard instead we press keys, not linked to any GameObject in particular.

So we listen for those events on this.input.keyboard, like this:

this.input.keyboard.on(<event>, function() {

})

<event> is a string that can be keyup or keydown, to intercept all keys pressed, or a combination of it with a letter identifying a specific key, like:

  • keyup-A
  • keyup-THREE
  • keydown-F12
  • keydown-ENTER

We have a large number of identifiers we can use, including:

  • A ~ Z
  • F1 ~ F12
  • BACKSPACE
  • TAB
  • ENTER
  • SHIFT
  • CTRL. ALT
  • PAUSE
  • CAPS_LOCK
  • ESC
  • SPACE
  • PAGE_UP, PAGE_DOWN
  • END, HOME
  • LEFT, UP, RIGHT,DOWN
  • PRINT_SCREEN
  • INSERT, DELETE
  • ZERO, ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE
  • NUMPAD_ZERO, NUMPAD_ONE, NUMPAD_TWO, NUMPAD_THREE, NUMPAD_FOUR, NUMPAD_FIVE, NUMPAD_SIX, NUMPAD_SEVEN, NUMPAD_EIGHT, NUMPAD_NINE, NUMPAD_ADD, NUMPAD_SUBTRACT

Instead of listenining on individual keys, we can get an object calling this.input.keyboard.createCursorKeys():

let cursors

function create() {
  cursors = this.input.keyboard.createCursorKeys()
}

and in the update() function, we can check if one specific key has been pressed, and do something when this happens:

function update() {
  if (cursors.right.isDown) {
    text.x += 5
  }
  if (cursors.left.isDown) {
    text.x -= 5
  }
  if (cursors.up.isDown) {
    text.y -= 5
  }
  if (cursors.down.isDown) {
    text.y -= 5
  }
}


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com


Print Share Comment Cite Upload Translate Updates
APA

flaviocopes.com | Sciencx (2021-04-20T05:00:00+00:00) Phaser: Keyboard events. Retrieved from https://www.scien.cx/2021/04/20/phaser-keyboard-events/

MLA
" » Phaser: Keyboard events." flaviocopes.com | Sciencx - Tuesday April 20, 2021, https://www.scien.cx/2021/04/20/phaser-keyboard-events/
HARVARD
flaviocopes.com | Sciencx Tuesday April 20, 2021 » Phaser: Keyboard events., viewed ,<https://www.scien.cx/2021/04/20/phaser-keyboard-events/>
VANCOUVER
flaviocopes.com | Sciencx - » Phaser: Keyboard events. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/20/phaser-keyboard-events/
CHICAGO
" » Phaser: Keyboard events." flaviocopes.com | Sciencx - Accessed . https://www.scien.cx/2021/04/20/phaser-keyboard-events/
IEEE
" » Phaser: Keyboard events." flaviocopes.com | Sciencx [Online]. Available: https://www.scien.cx/2021/04/20/phaser-keyboard-events/. [Accessed: ]
rf:citation
» Phaser: Keyboard events | flaviocopes.com | Sciencx | https://www.scien.cx/2021/04/20/phaser-keyboard-events/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.