Class: FKeyboard
A helper class to manage keyboard events. Events will be triggered on each frame.
Example
// Static usage
FKeyboard.on('ArrowUp', () => {
console.log('ArrowUp key pressed!')
})
// Instance usage
const keyboard = new FKeyboard()
keyboard.on('ArrowUp', () => {
console.log('ArrowUp key pressed!')
})
Constructors
new FKeyboard()
new FKeyboard(
scene
):FKeyboard
Parameters
• scene: FScene
Returns
Defined in
Methods
clear()
clear():
void
Remove all the listeners
Returns
void
Example
const keyboard = new FKeyboard(scene)
keyboard.on('ArrowUp', () => {
console.log('ArrowUp key pressed!')
})
keyboard.clear()
Defined in
on()
on(
key
,callback
): () =>void
Add a listener to a given key event. This is a small wrapper around the native addEventListener
method, which helps listen to key events continuously. Basically, this prevents keyboard "debounce" delay by firing the given callback on each frame if the key is pressed.
Parameters
• key: string
The key to listen to
• callback
The callback to call when the key is pressed
Returns
Function
The callback function that removes the listener
Returns
void
Examples
const keyboard = new FKeyboard()
keyboard.on('ArrowUp', () => {
console.log('ArrowUp key pressed!')
})
const keyboard = new FKeyboard()
const removeListener = keyboard.on('ArrowUp', () => {
console.log('ArrowUp key pressed!')
})
removeListener()
Defined in
onKeyDown()
onKeyDown(
key
,callback
): () =>void
Add a listener to a given key event when the key is pressed
Parameters
• key: string
The key to listen to
• callback
The callback to call when the key is pressed
Returns
Function
The callback function that removes the listener
Returns
void
Examples
const keyboard = new FKeyboard()
keyboard.onKeyDown('ArrowUp', () => {
console.log('ArrowUp key pressed!')
})
const keyboard = new FKeyboard()
const removeListener = keyboard.onKeyDown('ArrowUp', () => {
console.log('ArrowUp key pressed!')
})
removeListener()
Defined in
onKeyUp()
onKeyUp(
key
,callback
): () =>void
Add a listener to a given key event when the key is released
Parameters
• key: string
The key to listen to
• callback
The callback to call when the key is released
Returns
Function
The callback function that removes the listener
Returns
void
Examples
const keyboard = new FKeyboard()
keyboard.onKeyUp('ArrowUp', () => {
console.log('ArrowUp key released!')
})
const keyboard = new FKeyboard()
const removeListener = keyboard.onKeyUp('ArrowUp', () => {
console.log('ArrowUp key released!')
})
removeListener()
Defined in
Properties
callback
callback:
Record
<string
, () =>void
[]>
An map of all the callbacks for each key being pressed
Defined in
callbackKeyDown
callbackKeyDown:
Record
<string
, () =>void
[]>
An map of all the callbacks for each key being released
Defined in
callbackKeyUp
callbackKeyUp:
Record
<string
, () =>void
[]>
An map of all the callbacks for each key being released
Defined in
keys
keys:
Record
<string
,boolean
>
A map of all the keys being pressed