Skip to content

Instantly share code, notes, and snippets.

@cschaba
Forked from bebraw/keyboard.js
Created February 22, 2013 11:18
Show Gist options
  • Save cschaba/5012768 to your computer and use it in GitHub Desktop.
Save cschaba/5012768 to your computer and use it in GitHub Desktop.
var keyboard = function(window) {
var keyboard = window.keyboard || {};
if (!('isPressed' in keyboard)) {
keyboard._keys = {};
var handler = function(e, flag) {
var key = String.fromCharCode(e.which).toLowerCase();
keyboard._keys[key] = flag;
}
$(document).on('keypress', handler, true).
on('keyup', handler, false);
keyboard.isPressed = function(key) {
key = key.toLowerCase();
if(key in keyboard._keys) {
return keyboard._keys[key];
}
return false;
}
}
return keyboard;
}(window);
$(document).on('mousemove', function() {
if(keyboard.isPressed('a')) {
// do something cool now
console.log('The user pressed a while moving the mouse');
}
})
// basic API
shortcut.add(<hotkey>, <handler>, <options>);
// example
shortcut.add('a', function() {
// do something cool now
console.log('a was pressed!');
},
{
// don't trigger on input. see other opts too
disable_in_input: true
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment