Connecting Tech Pros Worldwide Forums | Help | Site Map

How to capture keyCode in FireFox?

Newbie
 
Join Date: Jul 2007
Posts: 3
#1: Jul 31 '07
Hi,

I need to capture Enter key (keyCode==13) in an designMode enabled iframe of my chat sending virtual textbox. it's a wysiwyg posting. However I have the following code working fine in IE7 but not working in FireFox.

Can Anybody please tell me how can I reffer to the keyCode in firefox in this example:

Expand|Select|Wrap|Line Numbers
  1. <body onLoad= Starta()>
  2. <script>
  3. function Starta() {
  4.     iF=document.getElementById('rtoIFrame0').contentWindow.document;
  5.     iF.onkeypress = cK;
  6.     return true;
  7. }
  8. function cK(event) {
  9.     if (document.getElementById('rtoIFrame0').contentWindow.event.keyCode){ 
  10.         k = document.getElementById('rtoIFrame0').contentWindow.event.keyCode; // Works on IE
  11.     }
  12.     else if (document.getElementById('rtoIFrame0').contentWindow.e.which){
  13.         k = document.getElementById('rtoIFrame0').contentWindow.e.which; // dos not Work on FireFox
  14.  
  15.     }
  16.     if(k==13){
  17.         alert(k);
  18.         return false;
  19.     }
  20.     return true;
  21. }
  22. </script>
  23. <iframe id=rtoIFrame0></iframe>

acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#2: Jul 31 '07

re: How to capture keyCode in FireFox?


You may need charCode instead. The method of obtaining the event is not correct. See this post.
Newbie
 
Join Date: Jul 2007
Posts: 3
#3: Aug 1 '07

re: How to capture keyCode in FireFox?


Hi acoder,

That was a nice script. However I tried to make it work in my special code but I didn't succeed. I can't reffer to the object by window.event once the object is located in an iframe.

The following part works on IE:
Expand|Select|Wrap|Line Numbers
  1. if (document.getElementById('rtoIFrame0').contentWind ow.event.keyCode){ 
  2. k = document.getElementById('rtoIFrame0').contentWindo  w.event.keyCode;
  3. }
Please just let me know what exactly should I write instead the following part to make it work on firefox too.
Expand|Select|Wrap|Line Numbers
  1. else if (document.getElementById('rtoIFrame0').contentWind ow.e.which){
  2. k = document.getElementById('rtoIFrame0').contentWindo w.e.which;
Thank You!
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#4: Aug 1 '07

re: How to capture keyCode in FireFox?


A simple
Expand|Select|Wrap|Line Numbers
  1. k = event.keyCode;
will be enough here.
Newbie
 
Join Date: Jul 2007
Posts: 3
#5: Aug 1 '07

re: How to capture keyCode in FireFox?


It didn't work too :( Does anybody have an idea what should I do?
Reply


Similar JavaScript / Ajax / DHTML bytes