How to capture keyCode in FireFox?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • samxx
    New Member
    • Jul 2007
    • 3

    How to capture keyCode in FireFox?

    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:

    Code:
    <body onLoad= Starta()>
    <script>
    function Starta() {
    	iF=document.getElementById('rtoIFrame0').contentWindow.document;
    	iF.onkeypress = cK;
    	return true;
    }
    function cK(event) {
    	if (document.getElementById('rtoIFrame0').contentWindow.event.keyCode){ 
    		k = document.getElementById('rtoIFrame0').contentWindow.event.keyCode; // Works on IE
    	}
    	else if (document.getElementById('rtoIFrame0').contentWindow.e.which){
    		k = document.getElementById('rtoIFrame0').contentWindow.e.which; // dos not Work on FireFox
    
    	}
    	if(k==13){
    		alert(k);
    		return false;
    	}
    	return true;
    }
    </script>
    <iframe id=rtoIFrame0></iframe>
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    You may need charCode instead. The method of obtaining the event is not correct. See this post.

    Comment

    • samxx
      New Member
      • Jul 2007
      • 3

      #3
      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:
      Code:
      if (document.getElementById('rtoIFrame0').contentWind ow.event.keyCode){ 
      k = document.getElementById('rtoIFrame0').contentWindo  w.event.keyCode;
      }
      Please just let me know what exactly should I write instead the following part to make it work on firefox too.
      Code:
      else if (document.getElementById('rtoIFrame0').contentWind ow.e.which){
      k = document.getElementById('rtoIFrame0').contentWindo w.e.which;
      Thank You!

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        A simple
        [CODE=javascript]k = event.keyCode;[/CODE] will be enough here.

        Comment

        • samxx
          New Member
          • Jul 2007
          • 3

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

          Comment

          Working...