-
-
Save cschaba/5012768 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).on('mousemove', function() { | |
if(keyboard.isPressed('a')) { | |
// do something cool now | |
console.log('The user pressed a while moving the mouse'); | |
} | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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