views:

3944

answers:

3

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?

+7  A: 

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:

  • F1 (Help),
  • F3 (Search),
  • F5 (Refresh),
  • F6 (focus address bar),
  • F7 (caret browsing mode),
  • F11 (full screen mode), and
  • F12 (used by several add-ons, including Firebug)

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.

William Brendel
Yes, I know that some keys are reserved. Never the less; I want to use whatever keys are not.
roosteronacid
On my computer, all the F-keys are reserved. Using Opera with some custom shortcut. Never EVER relies on "commonly unreserved keys" conventions.
gizmo
Is there a specific reason? I'm just finding it hard to think of a situation that would require the use of function keys. This is purely curiosity on my part; I'm not trying to talk you out of anything, merely suggesting alternatives.
William Brendel
“I want to use whatever keys are not” — Thing is, you can’t tell programmatically what keys aren’t reserved. Taking over the function keys might well be fine for your app, but it’s difficult to tell.
Paul D. Waite
+3  A: 

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).

mcherm
+1  A: 

I forget where I found it, but Jan Wolter wrote a great page on JavaScript keyboard events too: http://unixpapa.com/js/key.html

Paul D. Waite
This is easily the best resource I've seen for keyboard events in JavaScript. I use it frequently.
Tim Down