A jQuery plugin that will make you handle the keyboard like a Pro
<script type="text/javascript" src="js/jquery-1.12.0.min.js"></script> <script type="text/javascript" src="js/easykey-1.0.0.min.js"></script>
$(document) .onLeftArrowKeyDown(previousPage) .onRightArrowKeyDown(nextPage);
$(document) .onSKeyDown(save, $.easyKey.options.withControlPressed) .onSKeyUp(displaySaved, $.easyKey.options.withControlPressed));
$('#myInput').onF11KeyDown(function(e) { //do something in response to F11 being pressed //prevent the browser from going full screen e.preventDefault(); //prevent the event from bubbling up, //if you want to do both, just return false; //e.stopPropagation(); });
$(document).onSKeyDown(saveAll, $.easyKey.options.withControlPressed | $.easyKey.options.withShiftPressed);
Find more examples here
NOTE: If you need to handle a key that is not in the list, you can do this:
$(document).onKey(keyCode, function(e) { /* handle key here*/ }, options);
Where keyCode is a key code number, or a member of $.easyKey.keyCodes (for example $.easyKey.keyCodes.F11)
options allows you to specify if you want to handle key down ($.easyKey.options.onKeyDown), key up ($.easyKey.options.onKeyUp) or require that Alt, Control or Shift is pressed along with the key.
Here's how you combine several options, for example to handle pressing Control + F:
$(document).onKey($.easyKey.keyCodes.f, function(e) {/* handle key here*/}, $.easyKey.options.onKeyDown | $.easyKey.options.withControlPressed);
In this case, because F is in the list of the handled keys you can simply write:
$(document).onFKeyDown(function(e) {/* handle key here*/}, $.easyKey.options.withControlPressed);
Find more examples here