ASPSnippets

Alerts
Get notified when a new article is published.

Name
 
Email

Your email will always be private and will not be shared.

Follow us on twitter.
 
Event.keyCode Demystified
Author Name: Mudassar Khan Published Date: July 11, 2009
Filed Under :
JavaScript
Views: 5433

event.keycode not working in Mozilla?

How to use event.keycode in Mozilla?

How to use event.keycode for Firefox?

This is a common issue faced by many is using event.keyCode to get the key ASCII value in browsers other than Internet Explorer. So what to do?

But the fact is Mozilla Firefox has event.keyCode event only difference is the place where you use it

https://developer.mozilla.org/En/DOM/Event.keyCode

So now I’ll explain how to use event

 

ASCII Code on keypress event

 

<script type = "text/javascript">

    function GetKeyCode(evt)

    {

        var keyCode;

        if(evt.keyCode > 0)

        {

            keyCode = evt.keyCode;

        }

        else if(typeof(evt.charCode) != "undefined")

        {

            keyCode = evt.charCode;

        }

        alert("You pressed : " + keyCode);

    }

</script>

 

 

<asp:TextBox ID="TextBox1" runat="server" onkeypress = "GetKeyCode(event)" />

<input id="Text1" type="text" onkeypress = "GetKeyCode(event)"/>

 

 

    

Mozilla Firefox

Mozilla Firefox has the ASCII code of all keys in the event.keyCode whenever it is called on keyup and keydown events. But when you call it on keypress event it stores the ASCII codes of all keys that produce a character that is visibile like A - Z, 0 - 9, and other character keys in charCode while keyCode has ASCII codes of all non character keys like BACKSPACE, SHIFT, CTRL, etc.

 

Internet Explorer, Chrome, Safari

IE, Chrome and Safari has ASCII code of only character keys when called on keypress event.

 

Opera

In Opera you will get ASCII code of the all the keys in the event.keyCode event

So in case when you want to get ASCII codes on keypress event you will need to use the following function.

 

ASCII Code on keyup and keydown events

In keyup and keydown events you can get ASCII codes of all the keys in

Internet Explorer, Mozilla Firefox, Google Chrome, Opera and Apple Safari using the following JavaScript function

 

<script type = "text/javascript">

    function GetKeyCode(evt)

    {

        alert("You pressed : " + evt.keyCode);

    }

</script>

 

keyup

<asp:TextBox ID="TextBox1" runat="server" onkeyup = "GetKeyCode(event)" />

<input id="Text1" type="text" onkeyup = "GetKeyCode(event)"/>

 

keydown

<asp:TextBox ID="TextBox1" runat="server" onkeydown = "GetKeyCode(event)" />

<input id="Text1" type="text" onkeydown = "GetKeyCode(event)"/>



The above code has been tested in the following browsers

Internet Explorer  FireFox  Chrome  Safari  Opera 

* All browser logos displayed above are property of their respective owners.


Hope you liked it.


If you like this article, help us grow by bookmarking this page on any social bookmarking site.
Bookmark and Share Page copy protected against web site content infringement by Copyscape

Related Articles

Comments

Add Comments

You can add your comment about this article using the form below. Make sure you provide a valid email address
else you won't be notified when the author replies to your comment

Please note that all comments are moderated and will be deleted if they are
  • Not relavant to the article
  • Spam
  • Advertising campaigns or links to other sites
  • Abusive content.
There is no need to add BR tags. Simply press enter for new line

Name*  
Email*
Comment*  
Security code
Security code