locked
distinguish between numpad 4 + shift and right + shift RRS feed

  • Question

  • In my keydown event handler, when Keys.Numpad4 is pressed with Shift, I get the same keycode code as
    as for Keys.Right. Is this by design? How can I distinguish between the two?  

     


    Ryn
    Tuesday, December 15, 2009 3:16 PM

Answers

  • It is by design in the keyboard. (Its controlled by the NumLock state.)

    You should handle key press, in addition to key down. (And key up is far better than key down.)

    If you use key down and key press, then you can easily distinguish between the two. Here is what I mean:

        Private Sub TextBox2_KeyDown(ByVal sender As System.Object, _
                                     ByVal e As System.Windows.Forms.KeyEventArgs) _
                                     Handles TextBox2.KeyDown
            If e.Modifiers = Keys.Shift And _
                e.KeyValue = Keys.Right Then
                MsgBox("SHIFT + NumPad RIGHT")
            End If
        End Sub
    
        Private Sub TextBox2_KeyPress(ByVal sender As System.Object, _
                                      ByVal e As System.Windows.Forms.KeyPressEventArgs) _
                                      Handles TextBox2.KeyPress
            If e.KeyChar = "6" Then
                MsgBox("NumPad 6")
            End If
        End Sub
    
    • Proposed as answer by konikula Wednesday, December 16, 2009 7:53 PM
    • Marked as answer by Martin_XieModerator Tuesday, December 22, 2009 2:31 AM
    Wednesday, December 16, 2009 7:12 PM
  • If you'd needed just code of key on keyboard, which does not add any more sense to key (no 6 or Right for the same KeyboardKey), you can take e.KeyValue, which is just numeric, into game.

    best regards, matt
    • Proposed as answer by konikula Wednesday, December 16, 2009 7:53 PM
    • Marked as answer by Martin_XieModerator Tuesday, December 22, 2009 2:31 AM
    Wednesday, December 16, 2009 7:40 PM
  • KeyCode will give you the code of the keyboard key.  If you want the value of the key itself, use .KeyValue.
    • Proposed as answer by konikula Wednesday, December 16, 2009 7:53 PM
    • Marked as answer by Martin_XieModerator Tuesday, December 22, 2009 2:31 AM
    Wednesday, December 16, 2009 7:48 PM

All replies

  • It sounds like a faulty keyboard!   Keys.Right is the same as Numpad 6 with Shift.  The equivalents key to Numpad 4 with shift ought to be Key.Left (or, Numpad 4 without shift and with NumLock Off).   You should try areplacement keyboard.
    Tuesday, December 15, 2009 10:01 PM


  • Sorry, that was a typo.  I meant "when Keys.Numpad6 is pressed with Shift, I get the same keycode code as
    as for Keys.Right. Is this by design?"

    Is there a way to distiguish between the two??
    Ryn
    Wednesday, December 16, 2009 6:05 PM
  • It is by design in the keyboard. (Its controlled by the NumLock state.)

    You should handle key press, in addition to key down. (And key up is far better than key down.)

    If you use key down and key press, then you can easily distinguish between the two. Here is what I mean:

        Private Sub TextBox2_KeyDown(ByVal sender As System.Object, _
                                     ByVal e As System.Windows.Forms.KeyEventArgs) _
                                     Handles TextBox2.KeyDown
            If e.Modifiers = Keys.Shift And _
                e.KeyValue = Keys.Right Then
                MsgBox("SHIFT + NumPad RIGHT")
            End If
        End Sub
    
        Private Sub TextBox2_KeyPress(ByVal sender As System.Object, _
                                      ByVal e As System.Windows.Forms.KeyPressEventArgs) _
                                      Handles TextBox2.KeyPress
            If e.KeyChar = "6" Then
                MsgBox("NumPad 6")
            End If
        End Sub
    
    • Proposed as answer by konikula Wednesday, December 16, 2009 7:53 PM
    • Marked as answer by Martin_XieModerator Tuesday, December 22, 2009 2:31 AM
    Wednesday, December 16, 2009 7:12 PM
  • If you'd needed just code of key on keyboard, which does not add any more sense to key (no 6 or Right for the same KeyboardKey), you can take e.KeyValue, which is just numeric, into game.

    best regards, matt
    • Proposed as answer by konikula Wednesday, December 16, 2009 7:53 PM
    • Marked as answer by Martin_XieModerator Tuesday, December 22, 2009 2:31 AM
    Wednesday, December 16, 2009 7:40 PM
  • KeyCode will give you the code of the keyboard key.  If you want the value of the key itself, use .KeyValue.
    • Proposed as answer by konikula Wednesday, December 16, 2009 7:53 PM
    • Marked as answer by Martin_XieModerator Tuesday, December 22, 2009 2:31 AM
    Wednesday, December 16, 2009 7:48 PM