ASP Snippets

Categories






Alerts

Free Alerts

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




Follow us on twitter.




Zoom In | Zoom Out


Author is awarded Most Valuable Professional award by Microsoft ASP/ASP.Net

Event.keyCode Demystified

Author:Mudassar Khan

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.

Posted: Jul 11 2009, 02:25 by Mudassar Khan | Comments (1) RSS comment feed |
Views: 5084
Page copy protected against web site content infringement by Copyscape


If you like this article, help us grow by bookmarking this page on any social bookmarking site.
Bookmark and Share





Comments









Community News





Web Hosting SpotLight


Consulting


For consulting and work related queries click here.



Advertise


Advertise with us. For more details click here.


Suggestions


Please provide your valuable suggesstions here.

This Site is hosted on

Lunarpages.com Web Hosting