IE event and keyCode/charCode

Discussion in 'Javascript' started by Thomas Christensen, Oct 20, 2005.

  1. I'm trying to figure out what key the user pressed using a Danish
    keyboard layout. charCodeAt returns the correct number, but
    event.keyCode returns a wrong number, when using one of the keys that
    are different on a Danish keyboard layout. "-" returns 45 and 189
    respectively. Etc. (The input-field in the sample code below displays
    the right character).

    I've tried setting the lang-parameter to 'da', but it doesn't make a
    difference.

    sample code:

    <input type="text"
    onkeyup="alert(this.value.charCodeAt(this.value.length-1));alert(window.event.keyCode);">

    Am I missing something, or is the event.keyCode in IE hardcoded to an
    english keyboard layout? Is there a way to automatically convert the
    event.keyCode so it corresponds with the users keyboard?

    Thomas
     
    Thomas Christensen, Oct 20, 2005
    #1
    1. Advertising

  2. Thomas Christensen wrote:

    > I'm trying to figure out what key the user pressed using a Danish
    > keyboard layout. charCodeAt returns the correct number, but
    > event.keyCode returns a wrong number, when using one of the keys that
    > are different on a Danish keyboard layout. "-" returns 45 and 189
    > respectively. Etc. (The input-field in the sample code below displays
    > the right character).
    >
    > I've tried setting the lang-parameter to 'da', but it doesn't make a
    > difference.
    >
    > sample code:
    >
    > <input type="text"
    > onkeyup="alert(this.value.charCodeAt(this.value.length-1));alert(window.event.keyCode);">
    >
    > Am I missing something, or is the event.keyCode in IE hardcoded to an
    > english keyboard layout? Is there a way to automatically convert the
    > event.keyCode so it corresponds with the users keyboard?
    >
    > Thomas


    I am not sure if this answers your question, but I note that you are
    using "onkeyup". Try using "onkeypress", as the keyCode for this event
    captures the selected character (taking account of the shiftKey etc),
    whereas "onkeydown" and "onkeyup" only seem to capture raw information
    about the physical key you pushed on the keyboard.

    Julian
     
    Julian Turner, Oct 20, 2005
    #2
    1. Advertising

  3. Thomas Christensen

    RobG Guest

    Thomas Christensen wrote:
    > I'm trying to figure out what key the user pressed using a Danish
    > keyboard layout. charCodeAt returns the correct number, but
    > event.keyCode returns a wrong number, when using one of the keys that
    > are different on a Danish keyboard layout. "-" returns 45 and 189
    > respectively. Etc. (The input-field in the sample code below displays
    > the right character).


    This maybe a silly question, but are you trying to get the key that was
    pressed, or the character that was entered?

    Try some of these links:

    <URL:http://developer.mozilla.org/en/docs/keyCode>
    <URL:http://developer.mozilla.org/en/docs/DOM:event.charCode>

    <URL:http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/keycode.asp>

    the keyCode maps to the Unicode character of the keyboard key - that is,
    the actual key that was pressed. e.g. pressing 'u' gives a charCode of
    117 (lower case u) and a keyCode of 85 (the U key).

    Hold down the shift key, and you get 85 for both, which is the code for
    an upper case U character and the U key.

    charCode takes into account the whether modifier keys (shift, control,
    etc.) are pressed, keyCode doesn't.

    >
    > I've tried setting the lang-parameter to 'da', but it doesn't make a
    > difference.
    >
    > sample code:
    >
    > <input type="text"
    > onkeyup="alert(this.value.charCodeAt(this.value.length-1));alert(window.event.keyCode);">
    >
    >
    > Am I missing something, or is the event.keyCode in IE hardcoded to an
    > english keyboard layout? Is there a way to automatically convert the
    > event.keyCode so it corresponds with the users keyboard?


    Play with this:

    <input type="text" onkeyup="
    document.getElementById('zz').innerHTML =
    '<pre>charCodeAt: ' + this.value.charCodeAt(this.value.length-1)
    + '<br>keyCode :' + event.keyCode + '</pre>;
    ">

    <div id="zz"></div>



    --
    Rob
     
    RobG, Oct 20, 2005
    #3
  4. Thomas Christensen

    Robert Guest

    Thomas Christensen wrote:
    > I'm trying to figure out what key the user pressed using a Danish
    > keyboard layout.
    > <input type="text"
    > onkeyup="alert(this.value.charCodeAt(this.value.length-1));alert(window.event.keyCode);">
    >
    > Am I missing something, or is the event.keyCode in IE hardcoded to an
    > english keyboard layout? Is there a way to automatically convert the
    > event.keyCode so it corresponds with the users keyboard?


    The keyCode is not hardcoded to an english layout.
    For IE in keyDown and keyUp events the keyCode is taken from the keyCode
    constants.

    http://msdn.microsoft.com/library/d...en-us/vbenlr98/html/vamsckeycodeconstants.asp
    http://www.w3.org/TR/1999/WD-DOM-Level-2-19990923/events.html

    In keyPress it is the Unicode character (codepoint?).
    So maybe you can change your onkeyup to onkeypress.

    You can play a little to see what works here
    http://www.w3.org/2002/09/tests/keys.html

    Als keep in mind that Mozilla/Firefox works different with these events.
     
    Robert, Oct 20, 2005
    #4
  5. Thomas Christensen

    Guest

    Julian Turner wrote:
    > I am not sure if this answers your question, but I note that you are
    > using "onkeyup". Try using "onkeypress", as the keyCode for this event
    > captures the selected character (taking account of the shiftKey etc),
    > whereas "onkeydown" and "onkeyup" only seem to capture raw information
    > about the physical key you pushed on the keyboard.
    >
    > Julian


    It works, but onkeypress occurs before onkeyup and before the content
    of the event source (ie. the input field gets updated). I can work
    around that though. Thanks!
     
    , Oct 20, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marvin Massih

    "Keycode DLL not found or invalid keycode"

    Marvin Massih, Dec 31, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    2,161
    Marvin Massih
    Dec 31, 2004
  2. Nathan Sokalski
    Replies:
    4
    Views:
    4,270
    Nathan Sokalski
    Mar 11, 2006
  3. Nathan Sokalski

    Determining the Browser and event.keyCode vs. event.which

    Nathan Sokalski, Mar 10, 2006, in forum: ASP .Net Building Controls
    Replies:
    4
    Views:
    193
    Nathan Sokalski
    Mar 11, 2006
  4. trouble with charCode

    , May 4, 2006, in forum: Javascript
    Replies:
    1
    Views:
    132
    Richard Cornford
    May 4, 2006
  5. jh3an

    How can I alter key.charCode value ?

    jh3an, Mar 22, 2008, in forum: Javascript
    Replies:
    6
    Views:
    171
    Thomas 'PointedEars' Lahn
    Mar 23, 2008
Loading...

Share This Page