Detect shift-character keypress

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris Williams

    #1

    Detect shift-character keypress

    Hi all,

    I am becoming a little stressed with the age-old problem of detecting when a
    user types (for example) a quote character ("). On a UK keyboard, I can look
    for a 2 + shift mask; on US, single quote + shift mask.

    I don't want to have to know every single international keyboard layout just
    to know when a shifted character has been entered. If anybody has any ideas
    to share, I'd be very grateful!

    Thanks in advance,

    Chris.


  • VK

    #2
    Re: Detect shift-character keypress

    I'm missing the root of your problem.

    " (double quote) has ASCII-ISO code 34 (hex 22), and this fact has nothing
    to do with the current keyboard layout. The same for the rest of ASCII table
    (low 127 chars).

    What is your actual aim?


    Comment

    • Chris Williams

      #3
      Re: Detect shift-character keypress

      Hi VK, thanks for the fast response.

      I have a function that is called onMouseDown and I want to know when the
      user types a quote, even if not currently in a text editing element.

      I can detect that a key has been pressed and can, of course, get the keyCode
      and whether shift was pressed. I don't seem to be able to get the ASCII
      code, which -- as you point out -- is exactly what I want.

      Here's the code:

      document.onkeyd own = keyIsDown;
      function keyIsDown(e){
      if(!e)
      e = window.event;

      window.status = "key down: " + String.fromChar Code(e.keycode) + " ( " +
      e.keyCode + ")";
      }

      If I press '2' , I get 2 (50); if I press '"', I also get 2 (50).

      Please tell me I'm missing something simple!

      Cheers...

      Chris.

      "VK" <schools_ring@y ahoo.com> wrote in message
      news:416d5321$0 $28197$9b622d9e @news.freenet.d e...[color=blue]
      > I'm missing the root of your problem.
      >
      > " (double quote) has ASCII-ISO code 34 (hex 22), and this fact has nothing
      > to do with the current keyboard layout. The same for the rest of ASCII[/color]
      table[color=blue]
      > (low 127 chars).
      >
      > What is your actual aim?
      >
      >[/color]


      Comment

      • Chris Williams

        #4
        Re: Detect shift-character keypress

        Sorry, onMouseDown should be onkeydown!


        Comment

        • Philip Ronan

          #5
          Re: Detect shift-character keypress

          On 13/10/04 5:41 pm, Chris Williams wrote:
          [color=blue]
          > If I press '2' , I get 2 (50); if I press '"', I also get 2 (50).[/color]

          The onkeydown event will check for *any* key being pressed. That includes
          the shift key. So when you press your "2" key, a totally separate event is
          fired.

          Use the onkeypress event instead

          document.onkeyp ress = checkKey;
          function checkKey(e) {
          if (!e) e = window.event;
          window.status = "key down: " + String.fromChar Code(e.keyCode) + " ( " +
          e.keyCode + ")";
          }

          Phil
          --
          Philip Ronan
          phil.ronanzzz@v irgin.net
          (Please remove the "z"s if replying by email)


          Comment

          • Chris Williams

            #6
            Re: Detect shift-character keypress

            Thanks very much Philip -- I think my confusion arose after creating
            functions to capture mouse events, using onmousedown. I think I more-or-less
            automatically created what I thought would be the keyboard equivalent, which
            did not work!

            Thanks for taking the time to reply, I've got this sorted now.

            Cheers,

            Chris.

            "Philip Ronan" <phil.ronanzzz@ virgin.net> wrote in message
            news:BD93454E.2 3A30%phil.ronan zzz@virgin.net. ..[color=blue]
            > On 13/10/04 5:41 pm, Chris Williams wrote:
            >[color=green]
            > > If I press '2' , I get 2 (50); if I press '"', I also get 2 (50).[/color]
            >
            > The onkeydown event will check for *any* key being pressed. That includes
            > the shift key. So when you press your "2" key, a totally separate event is
            > fired.
            >
            > Use the onkeypress event instead
            >
            > document.onkeyp ress = checkKey;
            > function checkKey(e) {
            > if (!e) e = window.event;
            > window.status = "key down: " + String.fromChar Code(e.keyCode) + " ( " +
            > e.keyCode + ")";
            > }
            >
            > Phil
            > --
            > Philip Ronan
            > phil.ronanzzz@v irgin.net
            > (Please remove the "z"s if replying by email)
            >
            >[/color]


            Comment

            Working...