KeyEvent
Convenience class to detect key presses on the browser.
This class accepts a browser key event when constructed and listens for key presses.
JLS.events.KeyEvent
new KeyEvent(listenForKey, options)
listenForKey
Name | Type | Description |
---|---|---|
listenForKey | string | The key character or key code to listen for and trigger events. |
options
Name | Type | Default | Description |
---|---|---|---|
locale | string | en | The locale of the character or key. This doesn't do anything at the moment. |
listenForType | string | value | Sets the type of key to listen for. value specifies a character key, code specifies a keyboard key code to listen for. |
ignoreCase | boolean | false | Applies only when listenForType is set to value . When set to true , the case will be ignore and both uppercase and lowercase characters will trigger an event. When set to false , only the specified character (including its case type (lower/upper)) will trigger an event. |
let C_Key = new KeyEvent('c', { ignoreCase: true })
// Detecting a key press
C_Key.press = () => { console.log('c key pressed') }
// Detecting a key release or "keyup" event
C_Key.release = () => { console.log('c key released') }
Members
locale
The locale of the character or key.
listenForKey
The key character or key code to listen for and trigger events.
listenForType
Sets the type of key to listen for. value
specifies a character key, code
specifies a keyboard key code to listen for.
ignoreCase
Boolean that specifies whether or not to trigger key events for different case characters.
pressedKey
A reference to the key that was pressed when the event is triggered. Resets on keyup or release
.
isDown
Boolean that is set to true
only when the key is pressed.
isUp
Boolean that is set to true
only when the key is released and not being pressed.
press
A function that must be set in order to listen for key down events. See example at top of page for setting a press method.
release
A function that must be set in order to listen for key up events. See example at top of page for setting a release method.
Methods
subscribe()
Attaches keydown and keyup listeners to the key and starts listening for events. This method is called automatically on object construction. If unsubscribe()
method is called, subscribe()
will need to be called in order for key event detection to be enabled.
unsubscribe()
Detaches keydown and keyup listeners to the key and stops listening for events.
init()
Automatically called on object contruction, this method intializes the KeyEvent by subscribing and listening for key events.