Simply put, a cross-browser compatible set of constants for all of the different key codes that
handled in HTML keypress
, keydown
, and keyup
events. Make your event code readable.
None.
Include the src/keyevent.js
in your project.
Here's a very very crude example. Fit this properly into your project.
<html>
<head>...</head>
<body>
Some page...
...
<script type="text/javascript" src="[PATH_TO]/keyevent.js"></script>
...
</body>
</html>
An object named KeyEvent
that has a bunch of constants. For example: KeyEvent.DOM_VK_RETURN
or KeyEvent.DOM_VK_ESCAPE
, etc. Check out the src/keyevent.js
It is quite trivial.
npm install --save keyevent
or
npm install --save-dev keyevent
Then simply include the node_modules/keyevent/src/keyevent.js
in your HTML page/project.
You can install KeyEvent using bower.
bower install keyevent
Then simply include the bower_components/keyevent/src/keyevent.js
in your HTML page/project.
Using jQuery's event binding, here's a couple simple cross-browser compliant ways of handling key events.
$('#some-text-input')
.on('keypress', function (e) {
if (e.keyCode === KeyEvent.DOM_VK_ESCAPE) {
$(this).val('');
}
});
$('#some-text-input')
.off('keypress.ctrl-enter-return-submit')
.on('keypress.ctrl-enter-return-submit', function (e) {
if (e.ctrlKey) {
switch (e.keyCode) {
case KeyEvent.DOM_VK_RETURN:
case KeyEvent.DOM_VK_ENTER:
this.form.submit();
}
}
});