Detect Fn keys

I’ve read some about an event handler for detect the press of any “normal” keys as letters and so on.

How can detect also the Fn keys as F3, F5, F9… and even these keys with upper key (i.e. caps+F1 = F13).

of curse I like to work at least in the main updated browsers.

Thaks in advance.

I don’t think you can. The only code I know of to detect the keys returns the F-keys as 0. I think this is good. Otherwise you could potentially lock the person from closing their browser (lock the mouse and keyboard) – the F keys are a last resort sort of.

You’re probably better off to just use ctrl+1, ctrl+2, or something.

Of course, you can detect F-key like any other (event.keyCode value for Fkeys are in range 112-122). To test if shift is pressed use event.shiftKey:

if(event.keyCode == 112 && event.shiftKey)
   // shift + F1 pressed

But that’s IE-specific, isn’t it?

What makes you think it’s IE specific? :wink:

This script will allow you to test the codes returned by different key-related events. There are differences in how IE and standards-compliant browsers report the events. Function keys, since the browser may attach internal functions to them, can be problematic.

 <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
 <style type="text/css">
 td,th{border:2px solid #aaa;}
 <script type="text/javascript">
 var t_cel,tc_ln;
 if(document.addEventListener){ //code for Moz
   document.attachEvent("onkeydown",keyCapt); //code for IE
 function keyCapt(e){
   if(typeof window.event!="undefined"){
 	e=window.event;//code for IE
   }else if(e.type=="keyup"){
   }else if(e.type=="keypress"){
 <table id="tblOne">
 <th style="border:none;"></th><th>onkeydown</th><th>onkeyup</th><th>onkeypress</td>
 <button onclick="for(i=0;i<tc_ln;i++){t_cel[i].innerHTML='&nbsp;'};">CLEAR</button>

I was using keypress, but the events and sample you mention do the work.

Thanks very much.

“Function keys, since the browser may attach internal functions to them, can be problematic.”
yes, I Know, my intention is using some of the Fn keys not used mainly.
btw… would be nice trap the key and return nothing to the browser in order to avoid the pressumed action (i.e. F1=BrowserHelp)…

There’s no problem with capturing function key events, but you won’t be able to override the default action - only attach new (additional) functions to that action

Which is why, as you said, you wanna look for keys which are not already used - F12 and F7 are good in moz, safari and IE; F6, F9 and F10 are okay in Opera 8 - but those are just the default setup … whatever you choose, someone somewhere will have it configured to do something else.

Because ultimately, both Windows and Mac can assign almost any key to do almost anything in almost any application.

So … if you do function key scripting you should really add user preferences to let people change the key assignments for themselves. This is what I do with the keyboard navigation hotkey on my menu –

Note also that Opera can’t detect most modifier + key combinations - key events stop being reported if, for example, the ALT key is pressed down.