CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 1999
    Location
    Israel
    Posts
    101

    Virtual Key Codes

    Could you, please, help me.
    Is there any API function, which can give me different codes
    for Left and Right Shift, Ctrl, Alt?
    I know how to use GetAsyncKeyState and GetKeyboardState
    but they give the same Virtual Codes for them.

    Thank you in advance.


  2. #2
    Join Date
    May 1999
    Posts
    3,332

    Re: Virtual Key Codes

    from the online docs:
    "An application can use the virtual-key code constants VK_SHIFT, VK_CONTROL and VK_MENU as indices into the array pointed to by lpKeyState. This gives the status of the SHIFT, CTRL, or ALT keys without distinguishing between left and right. An application can also use the following virtual-key code constants as indices to distinguish between the left and right instances of those keys:

    VK_LSHIFT
    VK_RSHIFT
    VK_LCONTROL
    VK_RCONTROL
    VK_LMENU
    VK_RMENU

    These left- and right-distinguishing constants are available to an application only through the GetKeyboardState, SetKeyboardState, GetAsyncKeyState, GetKeyState, and MapVirtualKey functions. "

    Thus, check for VK_LSHIFT to find out if the left shift key has been pressed.
    This is defined as 0xA0 which is different from VK_SHIFT (0x10)


  3. #3
    Join Date
    Dec 1999
    Location
    Tulsa, Oklahoma
    Posts
    1

    Re: Virtual Key Codes

    I am having the same problem. I tried the last message and did not work. Has anyone a sample they have tried that works in VB5/6.


  4. #4
    Join Date
    May 1999
    Posts
    3,332

    Re: Virtual Key Codes


    option Explicit
    private Declare Function GetAsyncKeyState Lib "user32" (byval vKey as Long) as Integer
    Const VK_LSHIFT = &HA0
    Const VK_SHIFT = &H10

    private Sub Form_KeyUp(KeyCode as Integer, Shift as Integer)
    Dim lResult as Integer
    lResult = GetAsyncKeyState(VK_LSHIFT)
    List1.AddItem "LShift " & lResult, 0
    lResult = GetAsyncKeyState(VK_SHIFT)
    List1.AddItem "Shift " & lResult, 0

    End Sub





    set Form - keypreview to true.

    if GetAsyncKeyState(VK_LSHIFT) returns 1, then the LEFT shift key has been pressed.
    It will not return 1 (but 0), if the right shift key has been pressed.
    GetAsyncKeyState(VK_SHIFT) will always return 1 (if ANY one of the shift keys is pressed).

    This does not work in Win95/98.
    I have tested this in NT 4 with VB 6



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

Featured