Event.KeyboardInput Class
EventProvider using the keyboard.
Each event represent a key. each key has two _states: KEY_PRESSED
or KEY_RELEASED
List of keys:
- KEY_A
- KEY_B
- KEY_C
- KEY_D
- KEY_E
- KEY_F
- KEY_G
- KEY_H
- KEY_I
- KEY_J
- KEY_K
- KEY_L
- KEY_M
- KEY_N
- KEY_O
- KEY_P
- KEY_Q
- KEY_R
- KEY_S
- KEY_T
- KEY_U
- KEY_V
- KEY_W
- KEY_X
- KEY_Y
- KEY_Z
- KEY_0
- KEY_1
- KEY_2
- KEY_3
- KEY_4
- KEY_5
- KEY_6
- KEY_7
- KEY_8
- KEY_9
- KEY_F1
- KEY_F2
- KEY_F3
- KEY_F4
- KEY_F5
- KEY_F6
- KEY_F7
- KEY_F8
- KEY_F9
- KEY_F10
- KEY_F11
- KEY_F12
- KEY_BACKSPACE
- KEY_TAB
- KEY_ENTER
- KEY_SHIFT
- KEY_ALT
- KEY_PAUSE
- KEY_CAPSLOCK
- KEY_ESC
- KEY_SPACE
- KEYPAGEUP
- KEYPAGEDOWN
- KEY_END
- KEY_HOME
- KEY_LEFT
- KEY_UP
- KEY_RIGHT
- KEY_DOWN
- KEY_INSERT
- KEY_DELETE
- KEY_NUMLOCK
Note:
This class use the keyCode
attribute from KeyboardEvent object.
The code list of current W3C standard
is not implemented and there is differences between each browser.
The next W3C standard
should improve compatibility.
Currently, using Numeric key (from Keypad or not) with the Shift,
CapsLock or NumLock keys is not possible.
Use exotic keys are strongly discouraged.
For example, using Chrome 22.0.1229.94 (under Linux) with a fr keyboard, key &
(key 1
without Shift
)
are equally to key 7
.
For more information about compatibility,this document provide
a good summary of the situation.
Usage of arrows keys, and usage of controls keys with alphabetic characters are supported.
Constructor
Event.KeyboardInput
-
[target]
Parameters:
-
[target]
HTMLElement optionalelement to listen keypressed and keyup. default to
window.document
Example:
var keyboard = new KeyboardInput();
keyboard.addListener("KEY_SPACE", KeyboardInput.KEY_PRESSED, function(event, value, provider) {
if (provider.getState("KEY_CTRL") === KeyboardInput.KEY_PRESSED) {
//CTRL+space is pressed !
}
});
Methods
_getAssociatedEvent
-
event
search a state corresponding to the event object
Parameters:
-
event
KeyboardEvent
Returns:
_modifyState
-
event
-
newValue
Apply a modification to an internal state variable and call listeners.
Parameters:
-
event
Stringevent name
-
newValue
the new value.
_onKeyDown
-
event
Called when a key is pressed.
Parameters:
-
event
KeyboardEvent
_onKeyUp
-
event
Called when a key is released.
Parameters:
-
event
KeyboardEvent
addListener
-
[event]
-
[value]
-
callback
add a listener.
it can listen all events or only one event variable. The listener can choose to be called for all events associated to a variable, or only when the variable is in a certain state.
Parameters:
-
[event]
String optionalname of event variable. y default, all events are caught.
-
[value]
optionalvalue expected for call the callback. By default, any value call the callback.
-
callback
Functioncallback function called with 3 parameters:
-
event
Stringevent name
-
value
new value
-
provider
EventProviderinstance of provider
-
Returns:
Example:
//myCallback will be called for each events.
provider.addListener(myCallback);
//mySecondCallback will be called only when the "A" variable obtain the state KEY_PRESSED.
provider.addListener("A", provider.KEY_PRESSED, mySecondCallback);
getOldState
-
name
Search the previous state of a state variable. The provider keep always one old state for each variable. It's useful for compare the difference.
Parameters:
-
name
String
Returns:
getState
-
name
Search the state of a state variable
Parameters:
-
name
String
Returns:
getStateList
()
String
List all variables accessible by this provider Each variable can accept listeners.
Note: return value is a reference. you should make a copy if you need to modify it.
Returns:
getType
()
String
return the EventProvider type.
Returns:
rmListener
-
id
Remove a listener.
Parameters:
-
id
Numberid of the listener.
Properties
_oldValues
Array
protected
List of previous values for state variables.
this._states
and this._oldValues
share the same array index.
_states []
String
protected
List of all event variables name.
_values
Array
protected
List of values for state variables.
this._states
and this._values
share the same array index.
KEY_PRESSED
Boolean
static
Represent a key pressed state
KEY_RELEASED
Boolean
static
Represent a key released state