Results 1 to 4 of 4

Thread: determining EXACTLY which key was pressed...

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 1999
    Posts
    38

    Post

    how would i determine which key is pressed in a textbox

    the KeyDown event recieves the keycode of the key, however control and right control return the same number, as do alt and right alt, shift and right shift, and enter and num enter.

    what i am trying to do is, when a key is pressed, display the name of the key. when a key is pressed, say spacebar, the word 'Space' appears in the textbox. works fine, but the above mentioned keys must be treated differently.

  2. #2
    Addicted Member c@lle's Avatar
    Join Date
    Oct 1999
    Location
    Belgium
    Posts
    179

    Post

    Private Sub Form_KeyDown(keycode As Integer, shift As Integer)

    shift: An integer that corresponds to the state of the SHIFT, CTRL, and ALT keys at the time of the event. The shift argument is a bit field with the least-significant bits corresponding to the SHIFT key (bit 0), the CTRL key (bit 1), and the ALT key (bit 2 ). These bits correspond to the values 1, 2, and 4, respectively. Some, all, or none of the bits can be set, indicating that some, all, or none of the keys are pressed. For example, if both CTRL and ALT are pressed, the value of shift is 6.

    Code:
    Private Sub Text1_KeyDown (KeyCode As Integer, Shift As Integer)
       Dim ShiftDown, AltDown, CtrlDown, Txt
       ShiftDown = (Shift And vbShiftMask) > 0
       AltDown = (Shift And vbAltMask) > 0
       CtrlDown = (Shift And vbCtrlMask) > 0
       If KeyCode = vbKeyF2 Then   ' Display key combinations.
       If ShiftDown And CtrlDown And AltDown Then
          Txt = "SHIFT+CTRL+ALT+F2."
       ElseIf ShiftDown And AltDown Then
          Txt = "SHIFT+ALT+F2."
       ElseIf ShiftDown And CtrlDown Then
          Txt = "SHIFT+CTRL+F2."
       ElseIf CtrlDown And AltDown Then
          Txt = "CTRL+ALT+F2."
       ElseIf ShiftDown Then
          Txt = "SHIFT+F2."
       ElseIf CtrlDown Then
       Txt = "CTRL+F2."
       ElseIf AltDown Then
          Txt = "ALT+F2."
       ElseIf SHIFT = 0 Then
          Txt = "F2."
       End If
       Text1.Text = "You pressed " & Txt
       End If
    End Sub

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 1999
    Posts
    38

    Post

    thanks for the response, however i think you misunderstood me.

    on your keyboard there are 2 shift keys, 2 ctrl keys, 2 alt keys, one on the left side of the keyboard and one on the right side. also an enter key on the keyboard and an enter key on the 10-key. im not worried about whether or not any of the above keys are held down while entering a character, i just need to differentiate between the two, however the left shift key and the right shift key have the same keycodes, as do the left alt/right alt, left ctrl/right ctrl and enter/num enter. although they have the same value/function they are not the same keys, and i need to be able to tell exactly which one was pressed, the left one or the right one.

    i have seen this done in c++ programs, but im starting to think its not possible in vb.

    [This message has been edited by Howard Stern (edited 11-03-1999).]

  4. #4
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,176

    Post

    Try using the GetAsyncKeyState API which can monitor each individual key:
    Code:
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    Private Const VK_LCONTROL = &HA2
    Private Const VK_LSHIFT = &HA0
    Private Const VK_LMENU = &HA4
    
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
        If KeyCode = vbKeyShift Then
            Caption = IIf(GetAsyncKeyState(VK_LSHIFT), "Left Shift", "Right Shift")
        ElseIf KeyCode = vbKeyControl Then
            Caption = IIf(GetAsyncKeyState(VK_LCONTROL), "Left Control", "Right Control")
        ElseIf KeyCode = vbKeyMenu Then
            Caption = IIf(GetAsyncKeyState(VK_LMENU), "Left Alt", "Right Alt")
        End If
    End Sub

    ------------------
    Aaron Young
    Analyst Programmer
    aarony@redwingsoftware.com
    adyoung@win.bright.net

Posting Permissions

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



Click Here to Expand Forum to Full Width