A tiny JavaScript library let you bind function to keyboard.
Key.js
is using the API KeyboardEvent.key to detect which key is pressing/releasing.
See:
Include key.js
in your web app:
<script src="key.js"></script>
"Trigger" means the moment which the key is pressing down by the user.
// bind
Key.trigger('t').do(function(e) {
console.log('t is triggered', e);
});
// unbind
Key.trigger('t').unbind();
"Hold" means the time before the pressed key has been released.
// bind
Key.hold('h').do(function() {
console.log('h is holding');
});
// unbind
Key.hold('h').unbind();
The default value of the interval is one frame.(1000ms / 60, ~17ms)
To change the value of interval, use following method:
Key.hold('h').do(function() {
console.log('h is holding');
}).interval(1000); // interval is set to 1000ms
"Release" means the moment which the pressed key is releasing by the user.
// bind
Key.release('r').do(function(e) {
console.log('r is released', e);
});
// unbind
Key.release('r').unbind();
// bind
Key.combo('Control', 'q').do(function(e) {
console.log('Control + q combo', e);
});
Key.combo('Control', 'Alt', 'Enter').do(function(e) {
console.log('Control + Alt + Enter combo', e);
});
// unbind
Key.combo('Control', 'q').unbind();
Key.combo('Control', 'Alt', 'Enter').unbind();
Key.isHolding('i'); // return true or false