Solved

Capture ESCAPE and Function keys in Firefox with Javascript

Posted on 2009-05-06
7
2,301 Views
Last Modified: 2013-12-07
Hi,

I am attempting to write a web app that uses key presses for different things. I wish to be able to use the Escape key and Function in Javascript to call a funtion, and can in Internet Explorer and Google Chrome (not all function keys, but F6 onwards, which is enough), but can't in Firefox.

Now, what I am using it for will be only used on a certain number of computers, each of which I am able to modify settings on, so I am trying to find a way that I can either alter a Firefox setting, or install an extension, that will allow Javascript to use the Escape Key and the Function Keys (and possibly disable some of the default browser action of some of these, such as F3 which is search page).

The code that I have attached is what I am using to capture keys currently (goes in the <head> tag), and will capture F8. Also, this is simply copied and pasted code, but if there is a better way to capture the key (which I am sure there is), that would also be appreciated).
<script>
document.onkeydown = function getcode(ev) {
 
value = (window.external) ? event.keyCode : ev.keyCode;
 
//tells you the code of the button which was pressed
//F5 = 116, F8 = 119
 
if (window.external) {
		if (event.keyCode == 119 || value == 119) {
			alert("You pressed F8");
		}
	} else {
		if (ev.keyCode == 119 || value == 119) {
			alert("You pressed F8");
		}
	}
}
</script>

Open in new window

0
Comment
Question by:dh-s
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 8

Expert Comment

by:Haris V
ID: 24313543
please check this link,

1. http://www.cambiaresearch.com/c4/702b8cd1-e5b0-42e6-83ac-25f0306e3e25/Javascript-Char-Codes-Key-Codes.aspx

and


<html>

<head>

<script>
      function keypress()
      {
            if(event.keyCode == 27){ // Capture Esc key
                                            window.close();
            }
      }
</script>

</head>

<body onkeyup="keypress()">

</body>

</html>

0
 
LVL 10

Expert Comment

by:bugada
ID: 24313837
To be crossbrowser you need to modify your function as follow

<script>
      function keypress(e)
      {
         e = e || window.event;
         var keyCode = e.which || e.keyCode; 
         if(keyCode == 27){ // Capture Esc key
             window.close();
         }
      }
</script>

Open in new window

0
 

Author Comment

by:dh-s
ID: 24313905
The script supplied (even the cross browser one) works PERFECT in Internet Explorer, but not Firefox.

I need it to be able to work in Firefox... is there anything that I need to modify in the settings?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Accepted Solution

by:
bugada earned 125 total points
ID: 24313963
Following modern unobtrusive javascript, you can also write the code like this:
<script>
 
      document.onkeydown = function(e) {
         e = e || window.event;
         var keyCode = e.which || e.keyCode; 
         switch(keyCode) {
            case 27:
              alert("you pressed ESC");
              break;
            case 119:
              alert("you pressed F8");
              break;  
            default:
              alert("you pressed a not handled key: " + keyCode)
         }
      }
</script>

Open in new window

0
 
LVL 10

Expert Comment

by:bugada
ID: 24314012
remember that the code inside the if (window.external) { } block will never be executed by FF or other browser rather than IE.

If you don't need it, remove it, or make an else branch for other browsers.
0
 

Author Comment

by:dh-s
ID: 24314028
Hi, thanks bugada... that is the first code that I have tried that works in Firefox. Thanks.
0
 
LVL 10

Expert Comment

by:bugada
ID: 24314080
glad that helped you.

Another tip: if you want prevent the default action (like reloading the page after a F5 keypress) just add a return false; at the end of the function.
<script>
 
      document.onkeydown = function(e) {
         e = e || window.event;
         var keyCode = e.which || e.keyCode; 
         switch(keyCode) {
            case 27:
              alert("you pressed ESC");
              break;
            case 119:
              alert("you pressed F8");
              break;  
            default:
              alert("you pressed a not handled key: " + keyCode)
         }
         return false; // prevent default behaviour
      }
</script>

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
SSL stands for “Secure Sockets Layer” and an SSL certificate is a critical component to keeping your website safe, secured, and compliant. Any ecommerce website must have an SSL certificate to ensure the safe handling of sensitive information like…
Google currently has a new report that is in beta and coming soon to Webmaster Tool accounts. This Micro Tutorial will highlight new features for Google Webmaster Tools.
Shows how to create a shortcut to site-search Experts Exchange using Google in the Chrome browser. This eliminates the need to type out site:experts-exchange.com whenever you want to search the site. Launch the Search Engine Menu: In chrome, via you…

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question