https://github.com/cybertoothca/keyevent
The KeyEvent object gives us all the constants for key codes (should these not already exist somewhere?!?). See http://stackoverflow.com/a/1465409/545137
https://github.com/cybertoothca/keyevent
Last synced: 10 months ago
JSON representation
The KeyEvent object gives us all the constants for key codes (should these not already exist somewhere?!?). See http://stackoverflow.com/a/1465409/545137
- Host: GitHub
- URL: https://github.com/cybertoothca/keyevent
- Owner: cybertoothca
- License: mit
- Created: 2015-05-29T15:13:19.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2022-04-19T03:32:48.000Z (about 4 years ago)
- Last Synced: 2025-03-11T13:06:57.763Z (about 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 9.77 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# KeyEvent
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.
## Prerequisites
None.
## Installation
Include the `src/keyevent.js` in your project.
Here's a very very crude example. Fit this properly into your project.
```
...
Some page...
...
...
```
## What Do I Get?
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
```
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.
### Bower (this will continue to exist, but I'm using NPM instead)
You can install KeyEvent using bower.
```
bower install keyevent
```
Then simply include the `bower_components/keyevent/src/keyevent.js` in your HTML page/project.
## Example Usage
Using jQuery's event binding, here's a couple simple cross-browser compliant ways of handling key events.
### Escape Clears Text Input Value
```
$('#some-text-input')
.on('keypress', function (e) {
if (e.keyCode === KeyEvent.DOM_VK_ESCAPE) {
$(this).val('');
}
});
```
### Ctrl-Enter Submits The Inputs Form
```
$('#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();
}
}
});
```
## Useful Links
* [The Stack Overflow question that caused this...](http://stackoverflow.com/questions/1465374/javascript-event-keycode-constants/1465409#1465409)