https://github.com/gtramontina/keyvent.js
Keyboard events simulator
https://github.com/gtramontina/keyvent.js
Last synced: 9 months ago
JSON representation
Keyboard events simulator
- Host: GitHub
- URL: https://github.com/gtramontina/keyvent.js
- Owner: gtramontina
- Created: 2012-01-31T23:33:32.000Z (almost 14 years ago)
- Default Branch: master
- Last Pushed: 2013-12-11T17:24:06.000Z (about 12 years ago)
- Last Synced: 2025-03-18T13:32:11.380Z (9 months ago)
- Language: JavaScript
- Homepage:
- Size: 163 KB
- Stars: 46
- Watchers: 6
- Forks: 10
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Keyvent.js #
##### Keyboard events simulator. #####
[](https://travis-ci.org/gtramontina/keyvent.js)
### Examples
Simulate 'keydown' events on the 'document' element:
keyvent.down(13); // You can use key codes...
keyvent.down('enter'); // ... a few alias...
keyvent.down('shift a'); // ... combination of keys...
keyvent.down('⇧ a'); // ... and some sugar!
The same is valid for simulating 'keyup' events:
keyvent.up(13);
keyvent.up('enter');
keyvent.up('shift a');
keyvent.up('⇧ a');
If you want to simulate keyboard events on other DOM elements, you have to set a new context (please note that the 'on' function takes a raw DOM element as parameter):
var headerDiv = document.getElementById('headerDiv');
keyvent.on(headerDiv).down('control');
You may want to keep a copy of this context to use later:
var headerDivKeyboard = keyvent.on(headerDiv);
(...)
headerDivKeyboard.up('space');
The following aliases are supported (borrowed from [https://github.com/madrobby/keymaster](https://github.com/madrobby/keymaster)):
`⇧`, `shift`, `option`, `⌥`, `alt`, `ctrl`, `control`, `command`, `⌘`, `backspace`, `tab`, `clear`, `enter`, `return`, `esc`, `escape`, `space`, `up`, `down`, `left`, `right`, `home`, `end`, `pageup`, `pagedown`, `del`, `delete`, and `f1` through `f19`.
#### Notes
* The initial intent of this lib is to support testing, but you can find a better usage for it. :-)
* If you are using AMD (e.g. require.js) this lib becomes a module. Otherwise it'll create a global `keyvent`.
### Browser Compatibility
I've ran the tests in Chrome and Firefox.
If you find any incompatibility or want to support other browsers, please do a pull request with the fix! :-)
### License
This is licensed under the feel-free-to-do-whatever-you-want-to-do license.