Static class to manage the keyboard and other input devices that generate key codes. It will return itself if it is tried to be instantiated.
- Source:
Namespaces
Members
-
<static, readonly> keysDown :Object
-
Object that will store the status for each key detected, using the key code as index and a boolean as their value (true when down or false when released).
Type:
- Object
- Default Value:
-
- {}
- Source:
-
<static, readonly> keysPressed :array
-
Array with the codes of the keys pressed recently (it will be cleared after the chosen milliseconds set with the
CB_Keyboard.setKeysPressedExpirationfunction).Type:
- array
- Default Value:
-
- []
- Source:
-
<static, readonly> typedString :string
-
Stores the string typed recently (it will be cleared after the chosen milliseconds set with the
CB_Keyboard.setTypedStringExpirationfunction).Type:
- string
- Source:
-
<static, readonly> typedStringCodes :array
-
Array with the codes of the string typed recently (it will be cleared after the chosen milliseconds set with the
CB_Keyboard.setTypedStringExpirationfunction).Type:
- array
- Default Value:
-
- []
- Source:
Methods
-
<static> clearKeysDown(keepPressed)
-
Clears (empties totally or partially) the
CB_Keyboard.keysDownobject which contains the detected keys pressed or released.Parameters:
Name Type Description keepPressedboolean If set to true, it will keep the pressed keys.
- Source:
-
<static> clearKeysPressed(keepPressed)
-
Clears (empties totally or partially) the
CB_Keyboard.keysPressedarray which contains the key codes pressed recently.Parameters:
Name Type Description keepPressedboolean If set to true, it will keep the pressed keys (taken from the
CB_Keyboard.keysDownobject).- Source:
-
<static> clearTypedString(keepPressed)
-
Clears (empties totally or partially) the
CB_Keyboard.typedStringstring and theCB_Keyboard.typedStringCodesarray.Parameters:
Name Type Description keepPressedboolean If set to true, it will keep the values belonging to the currently-pressed keys (taken from the
CB_Keyboard.keysDownobject).- Source:
-
<static> getKeyCode(e [, avoidNormalize]) → {integer}
-
Returns the key code that is contained in the given keyboard event.
Parameters:
Name Type Argument Default Description eEvent Keyboard event object.
avoidNormalizeboolean <optional>
false If it is not set to true, it will call the
CB_Events.normalizefunction internally before.- Source:
- To Do:
-
- Have in mind that keyCode is deprecated.
Returns:
Returns the key code.
- Type
- integer
-
<static> getKeysDown() → {Object}
-
Returns the
CB_Keyboard.keysDownobject which contains the detected keys pressed or released.- Source:
Returns:
Returns the
CB_Keyboard.keysDownobject.- Type
- Object
-
<static> getKeysPressed() → {array}
-
Returns the
CB_Keyboard.keysPressedarray which contains the key codes pressed recently (it will be cleared after the chosen milliseconds set with theCB_Keyboard.setKeysPressedExpirationfunction).- Source:
Returns:
Returns the
CB_Keyboard.keysPressedarray.- Type
- array
-
<static> getKeysPressedExpiration() → {integer}
-
Returns the milliseconds after which the
CB_Keyboard.keysPressedarray is always cleared (emptied). The time always starts counting from zero again when a key is pressed (onKeyDown event is fired).
To define this amount of time, theCB_Keyboard.setKeysPressedExpirationmethod must be used.- Source:
Returns:
Returns the milliseconds of expiration defined for the
CB_Keyboard.keysPressedarray.- Type
- integer
-
<static> getTypedString() → {string}
-
Returns the
CB_Keyboard.typedStringstring which contains the string typed recently (it will be cleared after the chosen milliseconds set with theCB_Keyboard.setTypedStringExpirationfunction).- Source:
Returns:
Returns the
CB_Keyboard.typedStringstring.- Type
- string
-
<static> getTypedStringCodes() → {array}
-
Returns the
CB_Keyboard.typedStringCodesarray which contains the key codes pressed that belongs to the string typed recently (it will be cleared after the chosen milliseconds set with theCB_Keyboard.setTypedStringExpirationfunction).- Source:
Returns:
Returns the
CB_Keyboard.typedStringCodesarray.- Type
- array
-
<static> getTypedStringExpiration() → {integer}
-
Returns the milliseconds after which the
CB_Keyboard.typedStringstring and theCB_Keyboard.typedStringCodesarray are always cleared (emptied). The time always starts counting from zero again when a key is pressed (onKeyPress event is fired).
To define this amount of time, theCB_Keyboard.setTypedStringExpirationmethod must be used.- Source:
Returns:
Returns the milliseconds of expiration defined for the
CB_Keyboard.typedStringstring and theCB_Keyboard.typedStringCodesarray.- Type
- integer
-
<static> isKeyDown(keyCodes [, allPressed]) → {boolean}
-
Returns whether the given key codes are being pressed (any of them or all at the same time, depending on the "allPressed" parameter).
Parameters:
Name Type Argument Default Description keyCodesinteger | array | Object An integer with the key code or a numeric array with the key codes that we want to check. It can also be an array of arrays, being each element a numeric array with the key codes that we want to check. Although not recommended (for performance purposes), this parameter can also support an object whose indexes are the keycodes (it will be converted to a numeric array internally).
allPressedboolean <optional>
false If set true, the function will only return true in the case that all given key codes are currently being pressed. Otherwise, if set to false, the function will return true in the case that any of the given key codes is currently being pressed. When the "keyCodes" is an array of arrays with key codes, it will be considered that all keys are being pressed if each single array (all of them) has at least one of its key codes pressed.
- Source:
Returns:
If "allPressed" parameter is set to true, returns true in the case that all given key codes are currently being pressed. If "allPressed" parameter is set to false, returns true in the case that any of the given key codes is currently being pressed. In all other cases, it returns false.
- Type
- boolean
-
<static> normalizeEvent(e) → {Event}
-
Tries to return the given keyboard event with some properties normalized (since different clients can use different values) and perhaps some new properties added (in the case they were missing), when possible. The new attached methods and properties may include polyfills, etc. It also calls the
CB_Events.normalizefunction internally. Some properties added or affected could be keyCode, location, ctrlKey, altKey, shiftKey, etc.Parameters:
Name Type Description eEvent Keyboard event object. If not provided, it will use the value of "event", "window.event", "Event" or an empty object ("{}").
- Source:
- To Do:
-
- Calculate (if possible) the values for location, ctrlKey, altKey, etc. when added, to simulate the expected behaviour.
Returns:
Returns the keyboard event object normalized.
- Type
- Event
-
<static> onKeyDown(callbackFunction [, keepOldFunction] [, useCapture] [, target])
-
Sets a function to execute when a key is down (onKeyDown event) or removes it.
Parameters:
Name Type Argument Default Description callbackFunctionCB_Keyboard.EVENT_CALLBACK | null The function (event listener) that we want to execute when the event is fired. The first parameter received for this function will be the event object (already normalized by the
CB_Keyboard.normalizeEventfunction) and the second one will be the key code associated. If a null value is used, the event will be removed.keepOldFunctionboolean <optional>
true Defines whether we want to keep any possible previous event listener for the same target and event name or not.
useCaptureboolean <optional>
false Defines whether the event we want to add will use capture or not. This parameter will be effective only if the current client supports the addEventListener method and will be used as its third parameter.
targetObject <optional>
document The target where we want to attach the event listener.
- Source:
-
<static> onKeyPress(callbackFunction [, keepOldFunction] [, useCapture] [, target])
-
Sets a function to execute when a key is pressed (onKeyPress event) or removes it.
Parameters:
Name Type Argument Default Description callbackFunctionCB_Keyboard.EVENT_CALLBACK | null The function (event listener) that we want to execute when the event is fired. The first parameter received for this function will be the event object (already normalized by the
CB_Keyboard.normalizeEventfunction) and the second one will be the key code associated. If a null value is used, the event will be removed.keepOldFunctionboolean <optional>
true Defines whether we want to keep any possible previous event listener for the same target and event name or not.
useCaptureboolean <optional>
false Defines whether the event we want to add will use capture or not. This parameter will be effective only if the current client supports the addEventListener method and will be used as its third parameter.
targetObject <optional>
document The target where we want to attach the event listener.
- Source:
-
<static> onKeyUp(callbackFunction [, keepOldFunction] [, useCapture] [, target])
-
Sets a function to execute when a key is released (onKeyUp event) or removes it.
Parameters:
Name Type Argument Default Description callbackFunctionCB_Keyboard.EVENT_CALLBACK | null The function (event listener) that we want to execute when the event is fired. The first parameter received for this function will be the event object (already normalized by the
CB_Keyboard.normalizeEventfunction) and the second one will be the key code associated. If a null value is used, the event will be removed.keepOldFunctionboolean <optional>
true Defines whether we want to keep any possible previous event listener for the same target and event name or not.
useCaptureboolean <optional>
false Defines whether the event we want to add will use capture or not. This parameter will be effective only if the current client supports the addEventListener method and will be used as its third parameter.
targetObject <optional>
document The target where we want to attach the event listener.
- Source:
-
<static> preventF11Key(e)
-
Tries to prevent the default behaviour that would produce the "F11" key of a given keyboard event when the client is compatible with the HTML5 Fullscreen API and uses it to toggle (enable or disable) the fullscreen mode. It calls the
CB_Keyboard.normalizeEventfunction internally.Parameters:
Name Type Description eEvent Keyboard event object.
- Source:
-
<static> setKeysPressedExpiration(keysPressedExpiration) → {boolean}
-
Sets the milliseconds after which the
CB_Keyboard.keysPressedarray is always cleared (emptied). The time always starts counting from zero when a key is pressed (onKeyDown event is fired).Parameters:
Name Type Description keysPressedExpirationinteger An integer greater than 0 (zero) representing the milliseconds after which we desire that the
CB_Keyboard.keysPressedarray is cleared (emptied), if no key is pressed during this time.- Source:
Returns:
Returns true if the given time could be applied or false otherwise.
- Type
- boolean
-
<static> setTypedStringExpiration(keysPressedExpiration) → {boolean}
-
Sets the milliseconds after which the
CB_Keyboard.typedStringstring and theCB_Keyboard.typedStringCodesarray are always cleared (emptied). The time always starts counting from zero when a key is pressed (onKeyPress event is fired).Parameters:
Name Type Description keysPressedExpirationinteger An integer greater than 0 (zero) representing the milliseconds after which we desire that the
CB_Keyboard.typedStringstring and theCB_Keyboard.typedStringCodesarray are cleared (emptied), if no key is pressed during this time.- Source:
Returns:
Returns true if the given time could be applied or false otherwise.
- Type
- boolean
Type Definitions
-
EVENT_CALLBACK(e, keyCode)
-
Callback that is called before loading a file and should return true if we want to load the file or false otherwise.
Parameters:
Name Type Description eEvent Keyboard event object.
keyCodeinteger Key code which fired the event.
- Source: