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?
Try this solution if works.
My solution to this problem is:
With the magic number
123
which is the key F12.Solution in ES6 for modern browsers and IE11 (with transpilation to ES5):
One of the problems in trapping the F1-F12 keys is that the default function must also be overridden. Here is an example of an implementation of the F1 'Help' key, with the override that prevents the default help pop-up. This solution can be extended for the F2-F12 keys. Also, this example purposely does not capture combination keys, but this can be altered as well.
I borrowed a similar solution from a related SO article in developing this. Let me know if this worked for you as well.
I am not sure if intercepting function keys is possible, but I would avoid using function keys all together. Function keys are used by browsers to perform a variety of tasks, some of them quite common. For example, in Firefox on Linux, at least six or seven of the function keys are reserved for use by the browser:
The worst part is that different browsers on different operating systems use different keys for different things. That's a lot of differences to account for. You should stick to safer, less commonly used key combinations.
Without other external class you can create your personal hack code simply using
Another help for all, I think is this test page for intercept the keyCode (simply copy and past in new file.html for testing your event).
Here is a working demo so you can try it right here: