Skip to content

Instantly share code, notes, and snippets.

@tal
Created April 15, 2011 15:21
Show Gist options
  • Select an option

  • Save tal/921875 to your computer and use it in GitHub Desktop.

Select an option

Save tal/921875 to your computer and use it in GitHub Desktop.
var keyCodes = {
backspace: 8,
tab: 9,
enter: 13,
escape: 27,
left: 37,
up: 38,
right: 39,
down: 40,
delete: 46,
period: 190,
comma: 188,
semicolon: 186,
equal: 187,
dash: 189,
forwardSlash: 191,
backSlash: 221,
quote: 222
};
jQuery.fn.buttonDown = function(keys, onPress, other) {
if (!_.isArray(keys)) {
keys = [keys];
}
var onKeycodes = [];
$.each(keys, function(i,key){
if (keyCodes[key]) onKeycodes.push(keyCodes[key]);
});
if (typeof other != 'function') {
other = function() {};
}
this.keyup(function(e){
if (onKeycodes.indexOf(e.which) !== -1) {
return onPress(e);
} else {
return other(e);
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment