Namespace $.dom.KeyboardEvent
Holds a DOM Keyboard Event method which provides a simple way to
determine the key the user pressed.
Defined in: core-dom.js.
| Constructor Attributes | Constructor Name and Description |
|---|---|
|
Holds a DOM Keyboard Event method which provides a simple way to determine
the key the user pressed.
|
| Method Attributes | Method Name and Description |
|---|---|
| <static> |
$.dom.KeyboardEvent.getKey()
This method returns the character / key identifier for the current keyboard
event.
|
By default, the global KeyboardEvent.prototype object is extended to contain the method defined in this namespace. If the KeyboardEvent object is not available in the browser, then the Event.prototype object is extended.
The getKey() method uses the this object to refer
to the current event. As such, you need to pass the correct this
object.
In the examples provided we will assume that the objects defined are
already extended. Thus, code like ev.getKey() can be
written directly.
For more advanced keyboard event handling needs, please check out the $.dom.KeyboardEventListener class.
var klogger = function (ev) {
console.log(ev.type + ' key ' + ev.getKey());
};
window.addEventListener('keydown', klogger, false);
window.addEventListener('keypress', klogger, false);
window.addEventListener('keyup', klogger, false);
Some of the key identifiers this method returns are listed in the DOM 3 Events specification. The notable differences are that this method uses names for several of the Unicode characters in that list:
- 'U+0008' = 'Backspace'
- 'U+0009' = 'Tab'
- 'U+0018' = 'Cancel'
- 'U+001B' = 'Escape'
- 'U+0020' = 'Space'
- 'U+007F' = 'Delete'
Additionally, all Unicode characters of the form 'U+XXXX' are converted to the actual character. For example 'U+0053' becomes 'S'.
This method is known to work in Opera, Firefox, Chrome, Safari, MSIE 8 and Konqueror.
For more advanced keyboard event handling, please check $.dom.KeyboardEventListener.
var dialog_keydown = function (ev) {
var key = ev.getKey();
if (key == 'F1') {
// show help
} else if (key == 'Escape') {
// close dialog
} else if (key == 'Enter') {
// accept changes and close dialog
}
};
window.addEventListener('keydown', dialog_keydown, false);
- Returns:
- {String} The character / key identifier for the current keyboard event. If the key is unrecognized null is returned.
- See:
- $.dom.KeyboardEventListener The more advanced keyboard event compatibility layer.