SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    I'm not a human
    Join Date
    Aug 2006
    Location
    India
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to identify the event of arrow key pressed?

    hi,

    i want to identify the pressing of arrow keys and do some operation like traversing thru a table ..

    i tried the following code but it haven't returned anything when i pressed the arrow keys and also i want to know the keycode of the arrow keys on numpad.

    Code:
    <script type="text/javascript">
    function noNumbers(e)
    {
    var keynum
    var keychar
    var numcheck
    
    if(window.event) // IE
    {
    	keynum = e.keyCode
    	alert (keynum)
    }
    else if(e.which) // Netscape/Firefox/Opera
    {
    	keynum = e.which
    	alert (keynum)
    }
    keychar = String.fromCharCode(keynum)
    return (keychar)
    }
    </script>
    
    <form>
    <input type="text" onkeypress="return noNumbers(event)" />
    </form>
    What's the error why the arrow key events are not reflected??
    Regards,
    Vijay
    Follow me on twitter @vijaycbe
    World Holiday Calander || My Cricket Blog

  2. #2
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Viju,

    Have a look at this demo.

  3. #3
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In IE there is no keyCode returned from a keypress unless the key is a character key.
    In firefox, a keypress returns a charCode, but if the key is not a character the value is 0.
    Surprisingly, Opera behaves in this instance like IE.

    In all of these, keydown events return a keyCode that corresponds with the ascii value assigned to the key,
    so you need to catch keydown events and examine the event.keyCode to read non characters, like the arrow keys.

    But don't try this for an international audience- not all keyboards in the world assign the same codes to the non character keys.

    The keyCodes for the arrow keys on an english language keyboard are usually 37,38,39 and 40.
    Last edited by mrhoo; Feb 8, 2007 at 09:02. Reason: emphasis

  4. #4
    I'm not a human
    Join Date
    Aug 2006
    Location
    India
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi mrhoo,

    as you are stating ,i'm involving in a tool which needs the global audience and if the keycode is not same for all keyboards , is there any other common method to find which key has been pressed?

    i'm particularly need that!!!!!1


    so if anyone knows about this , pl post it.
    Regards,
    Vijay
    Follow me on twitter @vijaycbe
    World Holiday Calander || My Cricket Blog

  5. #5
    SitePoint Zealot
    Join Date
    Jul 2006
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Check out the spreadsheet.js file associated with this example. Although there are some errors with my onclick implementation, the actual traversing of the table using the arrow keys works fine.

    Dave


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •