I want to handle F1-F12 keys using JavaScript and jQuery.
I am not sure what pitfalls there are to avoid, and I am not currently able to test implementations in any other browsers than Internet Explorer 8, Google Chrome and Mozilla FireFox 3.
Any suggestions to a full cross-browser solution? Something like a well-tested jQuery library or maybe just vanilla jQuery/JavaScript?
This works for me.
if(code ==112) { alert("F1 was pressed!!"); return false; }
F2 - 113, F3 - 114, F4 - 115, and so fort.
Wow it is very simple. i'm blame to write this, why no one make it before?
The best source I have for this kind of question is this page: http://www.quirksmode.org/js/keys.html
What they say is that the key codes are odd on Safari, and consistent everywhere else (except that there's no keypress event on IE, but I believe keydown works).
When you use angular.js for handling events, you should use
ng-keydown
for preventingpause in developer
in chrome.I agree with William that in general it is a bad idea to hijack the function keys. That said, I found the shortcut library that adds this functionality, as well as other keyboard shortcuts and combination, in a very slick way.
Single keystroke:
Combination of keystrokes:
You can do this with jquery like this: