Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7

Thread: find keycodes

  1. #1
    Regular Coder
    Join Date
    Nov 2003
    Location
    Vancouver Island Canada
    Posts
    139
    Thanks
    0
    Thanked 0 Times in 0 Posts

    find keycodes

    A little page for finding keycodes.


    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>test bed</title>
    <script language="JavaScript">
    function checkKeycode(e) {
    var keycode;
    if (window.event) keycode = window.event.keyCode;
    else if (e) keycode = e.which;
    alert("keycode: [" + keycode + "]");
    }
    </script>
    </head>
    <body>
    <p>Type in here:
    <input type="text" size="10" value="" onKeyPress="checkKeycode(event)">
    </p>
    </body>
    </html>

  • #2
    Regular Coder
    Join Date
    Nov 2003
    Location
    Vancouver Island Canada
    Posts
    139
    Thanks
    0
    Thanked 0 Times in 0 Posts
    sorry if this was inaprpriate!!!!!!!!!!!

  • #3
    New Coder
    Join Date
    May 2003
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    No, I don't think it is inappropriate, although it is a bit simple.
    Sometimes I write such a thing to remember which keycode some key has.
    But now I can copy your code

  • #4
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    FWIW: Here's a Bookmarklet which displays the keycode in the status bar.

    javascript&#58;void(document.body.onkeypress=function(){window.status=event.keyCode;});

    .....Willy

  • #5
    Regular Coder
    Join Date
    Apr 2003
    Location
    Atlanta, GA
    Posts
    487
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here's a static method to add to String that will return the value of the key pressed on a given event, or something. I wrote this a while ago, and I can see that it needs a bit of work.

    Code:
    String.fromKeyCode = function(keyCode,evtType) {
        if (evtType && evtType.length && evtType.toLowerCase() == "keypress")
            return String.fromCharCode(keyCode);
        var keyDownChars = new Array(16);
            keyDownChars[8] = '[Bksp]';
            keyDownChars[9] = '[Tab]';
            keyDownChars[12] = '[N5+shift]';
            keyDownChars[13] = '[Enter]';
            keyDownChars.push('[Shift]','[Ctrl]','[Alt]','[Pause]','[CapsLock]');
            for (i=11;i;--i) keyDownChars.push('undefined');
            keyDownChars[27] = '[Esc]';
            keyDownChars.push(' ','[PgUp]','[PgDn]','[End]','[Home]','[Left]','[Up]','[Right]','[Down]');
            for (i=7;i;--i) keyDownChars.push('undefined');
            keyDownChars[45] = '[Ins]';
            keyDownChars[46] = '[Del]';
            keyDownChars.push(['0',')'],['1','!'],['2','@'],['3','#'],['4','$'],['5','%'],['6','^'],['7','&'],['8','*'],['9','(']);
            for (i=7;i;--i) keyDownChars.push('undefined');
            keyDownChars.push('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','[WinKey]');
            for (i=4;i;--i) keyDownChars.push('undefined');
            keyDownChars.push('0','1','2','3','4','5','6','7','8','9','*','+','undefined','-','.','/','[F1]','[F2]','[F3]','[F4]','[F5]','[F6]','[F7]','[F8]','[F9]','[F10]','[F11]','[F12]');
            for (i=62;i;--i) keyDownChars.push('undefined');
            keyDownChars[144] = '[NumLock]';
            keyDownChars[145] = '[ScrollLock]';
            keyDownChars.push([';',':'],['=','+'],[',','<'],['-','_'],['.','>'],['/','?'],['`','~']);
            for (i=26;i;--i) keyDownChars.push('undefined');
            keyDownChars.push(['[','{'],['\\','|'],[']','}'],["'",'"']);
        return keyDownChars[keyCode];
    }
    Here's an example of use:
    Code:
    function eventHandler(evt) {
        var key = String.fromKeyCode((evt? evt.which : window.event.keyCode), (evt? evt.type; window.event.type));
        alert(key);
    }

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,950
    Thanks
    0
    Thanked 236 Times in 233 Posts
    The 'Key Events' link in my sig leads to a page that has complete display of key events and keycodes.

  • #7
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    123 is my favourite
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark


  •  

    Posting Permissions

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