Keyboard
Keyboard Functions
There are two ways you can get information from the keyboard: Event Listeners and Direct Polling. With an Event Listener, you write a function that gets called automatically when a key is pressed or released. With Direct Polling, you can ask whether a key is pressed whenever you want, which you would typically do during each frame in your update world function.
You can check out the list of key names to find the official names of each keyboard key.
- Keyboard Direct Polling
isKeyPressed
isKeyPressed
Returns whether a given keyboard key is currently pressed or not.
key
*
string
-
A string indicating which key to check
Returns
boolean
a boolean where true
means that key is currently pressed and false
means it is not
- Keyboard Event Listeners
onKeyPress
onKeyPress
Registers your function that listens for mouse pressed events.
listenerFunction
*
function
The listener function that you wrote. Takes one argument: the world.
key
*
string
A string indicating which key listen for
Returns
nothing
onAnyKeyPress
onAnyKeyPress
Registers your function that listens for any key pressed events. This function will get called once each time any key is pressed.
listenerFunction
*
function
The listener function that you wrote. Takes twos arguments: the world and the key that was pressed
Returns
nothing
onKeyRelease
onKeyRelease
Registers your function that listens for key released events. This function will get called once each time a particular key is released.
listenerFunction
*
function
The listener function that you wrote. Takes one argument: the world.
key
*
string
A string indicating which key listen for
Returns
nothing
onAnyKeyRelease
onAnyKeyRelease
Registers your function that listens for any key released events. This function will get called once each time any key is released.
listenerFunction
*
function
The listener function that you wrote. Takes twos arguments: the world and the key that was released
Returns
nothing
- Keyboard Utility Functions
sameKeys
sameKeys
Checks whether two names for keyboard keys refer to the same key
key1
*
number
-
The first key to compare
key2
*
number
-
The second key to compare
Returns
boolean
true
means that key is currently pressed and false
means it is not
getKeyName
getKeyName
Returns a name for a key
key
*
number
-
A number that was passed to your key pressed listener or your key released listener.
Returns
string
A string that names the key that was pressed or released
Last updated