Our AJAX app intercepts F9 to provide context sensitive help. It works in IE7+, Chrome, Safari and Opera, but not in Firefox. Help!
We researched IE, Firefox, Chrome, Safari and Opera - looking for a common function key that was unused to utilize for our app's context help key - and settled on the F9 key. Is there a way to ensure that the F9 key-press can be passed to the page -- it appears to do nothing in Firefox, but is not passed to the page. -- Thanks!
פתרון נבחר
Firefox does not populate window.event like IE does. It never has. Modern browsers can pass the event object from the inline handler. Here's an example. I don't know whether older browsers support this (it works in IE8):
<!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1"> <title>Test F9 keydown inline handler</title> </head> <body> <p>Try F9<br><input type="text" name="textinput" size="25" value="sample value" onkeydown="keys(event,100)"></p> <script type="text/javascript"> function keys(evt, arg){ if (evt.which == 120 || evt.keyCode == 120){ // F9 key alert("Function passed: "+arg); } return false; } </script> </body> </html>Read this answer in context 👍 1
כל התגובות (4)
For some reason I can't recall, I have a page with a script that detects F9 and Shift+F9 using the keyup event. It works for me in Firefox 21 on Windows 7 -- I can't vouch for "IceWeasel/3.5.16" which is entirely unfamiliar to me...
If you drop this into a page, does it work on your Firefox?
function keys(key){ if (!key) { key = window.event; key.which = key.keyCode; } if (key.which == 120){ // F9 key if (key.shiftKey){ alert("Shift+F9 pressed!"); } else { alert("F9 pressed without Shift!"); } } return false; } document.onkeyup = keys; document.body.focus();
If the IceWeasel detection is incorrect, see: How to reset the default user agent on Firefox.
Thanks jscher2000,
The function you provided, when added to out standard table maintenance html, traps the keypress - without even referencing it anywhere! But, I'm still having trouble getting it to work within a function referenced by the onkeydown property of an element...
Here is the code we have working with other browsers to pop-up contextual help...
The function: function checkKeydown(fnr) {
// Get Field Specs String from fspecs array fhelp = fspecs[fnr]; // if (window.event.keyCode==120) alert('Caught F9 keypress'); if (window.event.keyCode==120) create_HelpWindow(fhelp);
}
This is the snippet where we are dynamically building input boxes and we provide the field number as a pointer into the field specifications array: inputline+=' onkeydown="checkKeydown(\+ fnr + '\')"'
So, when a user has the focus in a specific input box, they press the F9 key and a pop-up box containing help about that specific field is displayed... But that approach it isn't working in Firefox...
פתרון נבחר
Firefox does not populate window.event like IE does. It never has. Modern browsers can pass the event object from the inline handler. Here's an example. I don't know whether older browsers support this (it works in IE8):
<!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1"> <title>Test F9 keydown inline handler</title> </head> <body> <p>Try F9<br><input type="text" name="textinput" size="25" value="sample value" onkeydown="keys(event,100)"></p> <script type="text/javascript"> function keys(evt, arg){ if (evt.which == 120 || evt.keyCode == 120){ // F9 key alert("Function passed: "+arg); } return false; } </script> </body> </html>
Thank you for the solution! It works! - (in all the browsers)...