Resolved: When Ascii and KeyCode can't distinguish left from right, then what?-VBForums
Results 1 to 18 of 18

Thread: Resolved: When Ascii and KeyCode can't distinguish left from right, then what?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2008
    Posts
    118

    Resolved: When Ascii and KeyCode can't distinguish left from right, then what?

    G'day guys.
    My app relies on key events however neither Ascii nor KeyCode support the distinction of left & right with the "Shift", "Ctrl", "Alt" or "Win" keys.
    For example : Left Shift, Right Shift, Left Ctrl, Right Ctrl,.... etc.

    Ascii can't even detect the arrow keys and KeyCode isn't much better.
    It has to be possible as games made back in 2002 can distinguish left from right with each and every one of those above mentioned keys.

    Is there another alternative to Ascii and KeyCode in vb6?

    Any help will be appreciated. Thanks.
    Last edited by AccadaccA; Apr 22nd, 2017 at 05:51 AM. Reason: Resolved
    I rely heavily on trial and error, mostly only succeeding in the latter.

  2. #2
    Frenzied Member
    Join Date
    Jun 2015
    Posts
    1,091

    Re: When Ascii and KeyCode can't distinguish left from right, then what?

    you an try GetAsyncKeyState

    https://msdn.microsoft.com/en-us/lib...=vs.85%29.aspx

    using VK_LSHIFT and VK_RSHIFT


    Code:
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
        If GetAsyncKeyState(VK_LSHIFT) < 0 Then lblLShift.BackColor = vbGreen
        If GetAsyncKeyState(VK_RSHIFT) < 0 Then lblRShift.BackColor = vbGreen
    End Sub
    
    Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
        If Not GetAsyncKeyState(VK_LSHIFT) < 0 Then lblLShift.BackColor = BackColor
        If Not GetAsyncKeyState(VK_RSHIFT) < 0 Then lblRShift.BackColor = BackColor
    End Sub
    Last edited by DEXWERX; Apr 20th, 2017 at 10:24 AM.

  3. #3
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    2,318

    Re: When Ascii and KeyCode can't distinguish left from right, then what?

    Here's a little sample program that shows it all.

    Just throw it into a Form1's code, and run it, and start hitting keys.

    Code:
    
    Option Explicit
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    Const VK_LSHIFT = &HA0&
    Const VK_RSHIFT = &HA1&
    Const VK_LCONTROL = &HA2&
    Const VK_RCONTROL = &HA3&
    Const VK_LMENU = &HA4&
    Const VK_RMENU = &HA5&
    
    Private Sub Form_Load()
        KeyPreview = True
    End Sub
    
    Private Property Get MouseLeftDown() As Boolean
        ' This returns the Windows state (regardless of the mouse hardware).
        MouseLeftDown = (GetAsyncKeyState(vbKeyLButton) And &H8000)
    End Property
    
    Public Property Get MouseRightDown() As Boolean
        ' This returns the Windows state (regardless of the mouse hardware).
        MouseRightDown = (GetAsyncKeyState(vbKeyRButton) And &H8000)
    End Property
    
    Private Property Get ShiftLeftDown() As Boolean
        ShiftLeftDown = (GetAsyncKeyState(VK_LSHIFT) And &H8000)
    End Property
    
    Public Property Get ShiftRightDown() As Boolean
        ShiftRightDown = (GetAsyncKeyState(VK_RSHIFT) And &H8000)
    End Property
    
    Private Property Get CtrlLeftDown() As Boolean
        CtrlLeftDown = (GetAsyncKeyState(VK_LCONTROL) And &H8000)
    End Property
    
    Public Property Get CtrlRightDown() As Boolean
        CtrlRightDown = (GetAsyncKeyState(VK_RCONTROL) And &H8000)
    End Property
    
    Private Property Get AltLeftDown() As Boolean
        AltLeftDown = (GetAsyncKeyState(VK_LMENU) And &H8000)
    End Property
    
    Public Property Get AltRightDown() As Boolean
        AltRightDown = (GetAsyncKeyState(VK_RMENU) And &H8000)
    End Property
    
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
        Debug.Print "-----------------"
        Debug.Print "Left  Shift Down : "; ShiftLeftDown
        Debug.Print "Right Shift Down : "; ShiftRightDown
        Debug.Print "Left  Ctrl  Down : "; CtrlLeftDown
        Debug.Print "Right Ctrl  Down : "; CtrlRightDown
        Debug.Print "Left  Alt   Down : "; AltLeftDown
        Debug.Print "Right Alt   Down : "; AltRightDown
    End Sub
    
    Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
        Debug.Print "-----------------"
        Debug.Print "Left  Mouse Down: "; MouseLeftDown
        Debug.Print "Right Mouse Down: "; MouseRightDown
    End Sub
    
    
    Enjoy,
    Elroy

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Feb 2008
    Posts
    118

    Re: When Ascii and KeyCode can't distinguish left from right, then what?

    Thanks for your reply DEXWERX but I'm getting "Right Shift" for both shift keys.
    I rely heavily on trial and error, mostly only succeeding in the latter.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Feb 2008
    Posts
    118

    Re: When Ascii and KeyCode can't distinguish left from right, then what?

    Thanks Elroy,
    That looks promising.
    I'm overdue for a new keyboard as my shift keys tend to stick. lol Timing. I almost bought a new keyboard yesterday.

    I'll buy the keyboard when the shops open and give your code a good workout. It's after 2am here.
    I rely heavily on trial and error, mostly only succeeding in the latter.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Feb 2008
    Posts
    118

    Re: When Ascii and KeyCode can't distinguish left from right, then what?

    Hmm, I bought a brand new keyboard and the above code still isn't doing as I'm expecting.
    I created a new form with just the code Elroy posted.

    I pressed Left Shift and it wrote "Right Alt Down: False"
    So I pressed Right Alt and it said "Right Alt Down: True"

    But it doesn't change with any other key. What am I missing?
    I would like the output to go to a label.

    After closing and starting the form again I noticed that it's always printing "Right Alt Down: False" even without touching any of the keys.
    I rely heavily on trial and error, mostly only succeeding in the latter.

  7. #7
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    15,558

    Re: When Ascii and KeyCode can't distinguish left from right, then what?

    Both your statements sound odd, especially the one where you said stuff is printing out without any keystrokes.

    Out of curiosity what does the Shift parameter in the Form_KeyDown event report when you press the shift or control key? You can also test for a couple other things:
    Debug.Print Shift And vbShiftMask, Shift And vbCtrlMask
    Insomnia is just a byproduct of, "It can't be done"

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {GDI+ Classes/Samples} {Unicode Open/Save Dialog} {Icon Organizer/Extractor}
    {VB and DPI Tutorial} {XP/Vista Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Feb 2008
    Posts
    118

    Re: When Ascii and KeyCode can't distinguish left from right, then what?

    Quote Originally Posted by LaVolpe View Post
    Out of curiosity what does the Shift parameter in the Form_KeyDown event report when you press the shift or control key?
    Sorry, I'm unsure if I follow what you're asking as I copy and pasted the exact code Elroy did.
    Code:
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
        Debug.Print "-----------------"
        Debug.Print "Left  Shift Down : "; ShiftLeftDown
        Debug.Print "Right Shift Down : "; ShiftRightDown
        Debug.Print "Left  Ctrl  Down : "; CtrlLeftDown
        Debug.Print "Right Ctrl  Down : "; CtrlRightDown
        Debug.Print "Left  Alt   Down : "; AltLeftDown
        Debug.Print "Right Alt   Down : "; AltRightDown
    End Sub
    The ONLY key that seems to work is the Right Alt. No other key has any inpact in the debug print.
    The mouse button down events work but again it only says "Right Mouse Down" with either "True" or "False" regardless of which mouse button is pressed..


    Okay whilst writing this I stopped to swap the order of the left and right mouse button parameters in the Form_MouseDown event.
    Code:
    Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
        Debug.Print "-----------------"
        Debug.Print "Right Mouse Down: "; MouseRightDown
        Debug.Print "Left  Mouse Down: "; MouseLeftDown
    End Sub
    Now it's calling both mouse buttons as "Left".

    Like with the key down events, it is hanging onto the last line of each sub.
    Last edited by AccadaccA; Apr 21st, 2017 at 09:42 PM. Reason: typo
    I rely heavily on trial and error, mostly only succeeding in the latter.

  9. #9
    PowerPoster
    Join Date
    Dec 2004
    Posts
    22,436

    Re: When Ascii and KeyCode can't distinguish left from right, then what?

    on testing with no code changes, i found keypresses all worked correctly to start, then would only work on second keypress, then stop altogether, however mouse buttons would still work correctly, after mouebutton, keypress would work again for a while

    i am sure there may be some differences on other hardware or OS
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Feb 2008
    Posts
    118

    Re: When Ascii and KeyCode can't distinguish left from right, then what?

    Thanks for testing it westconn1.
    So it's not just me being insane after all. Who would have guessed? lol
    I rely heavily on trial and error, mostly only succeeding in the latter.

  11. #11
    Hyperactive Member
    Join Date
    Jul 2007
    Location
    Essex, UK.
    Posts
    322

    Re: When Ascii and KeyCode can't distinguish left from right, then what?

    I think what is happening here is that you don't have the immediate window expanded enough. When you use Elroy's code it loops through all the the possible keystates and prints them all finishing with 'AltRightDown'.

    Use your mouse to lift the top of the immediate window so you see at least 6 lines.

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Feb 2008
    Posts
    118

    Re: When Ascii and KeyCode can't distinguish left from right, then what?

    Quote Originally Posted by Steve Grant View Post
    I think what is happening here is that you don't have the immediate window expanded enough. When you use Elroy's code it loops through all the the possible keystates and prints them all finishing with 'AltRightDown'.

    Use your mouse to lift the top of the immediate window so you see at least 6 lines.
    Spot on, Steve. That was it.
    Cheers buddy.
    Last edited by AccadaccA; Apr 22nd, 2017 at 05:55 AM.
    I rely heavily on trial and error, mostly only succeeding in the latter.

  13. #13
    PowerPoster
    Join Date
    Dec 2004
    Posts
    22,436

    Re: Resolved: When Ascii and KeyCode can't distinguish left from right, then what?

    I think what is happening here is that you don't have the immediate window expanded enough
    i was running with the immediate window at about 2/3 screen height, the window scrolls as data is added at the bottom, so you can always see the most recent, and when pressing different keys can see that the text does not always change by the currently pressed key

    i ran a new test, even making sure to space out the interval of my keypresses in case the event was not firing quickly enough, results are the same
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  14. #14
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    2,318

    Re: Resolved: When Ascii and KeyCode can't distinguish left from right, then what?

    Ahhh, AccadaccA, glad you got it sorted.

    I guess I should have put a Select Case statement in the KeyDown event, and only printed the key that was actually pressed.

    Happy Coding,
    Elroy

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Feb 2008
    Posts
    118

    Re: Resolved: When Ascii and KeyCode can't distinguish left from right, then what?

    Thanks Elroy, it's all good.
    I just changed the KeyDown event to suit my needs..
    Code:
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
        If ShiftLeftDown = True Then lblMEvent.Caption = "Left Shift"
        If ShiftRightDown = True Then lblMEvent.Caption = "Right Shift"
        If CtrlLeftDown = True Then lblMEvent.Caption = "Left Ctrl"
        If CtrlRightDown = True Then lblMEvent.Caption = "Right Ctrl"
        If AltLeftDown = True Then lblMEvent.Caption = "Left Alt"
        If AltRightDown = True Then lblMEvent.Caption = "Right Alt"
    End Sub
    And it works like a charm.
    I'm not concerned with the mouse events as I already have them covered including middle (3rd) button and scroll Up & Down.

    Thanks again (all who replied), much appreciated.
    I rely heavily on trial and error, mostly only succeeding in the latter.

  16. #16
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    2,318

    Re: Resolved: When Ascii and KeyCode can't distinguish left from right, then what?

    Say AccadaccA,

    Just an FYI. The way you did it (reporting in a label's caption), you're only going to "see" the last key that's actually down in your series of "if" statements. The way I did it (reporting in the debug window), you will know when two or more keys are down.

    To stick with your approach, you could clear your label at the beginning, and then append (lblMEvent.Caption = lblMEvent.Caption & "some key" & vbCrLf) the key that was actually pressed. That way, you would get a full report of all the keys that were down.

    You would want to make sure your label had enough height to report all the keys you may be pressing.

    Regards,
    Elroy

  17. #17

    Thread Starter
    Lively Member
    Join Date
    Feb 2008
    Posts
    118

    Re: Resolved: When Ascii and KeyCode can't distinguish left from right, then what?

    Thanks again Elroy.
    I appreciate you taking this further with your suggestion but it is only going to be used as key binding for a game.
    So if the user has fat fingers that press several keys at the same time they needn't play the game... unless they intend to jump and crouch at the same time. lol

    Have a good one.
    I rely heavily on trial and error, mostly only succeeding in the latter.

  18. #18

    Thread Starter
    Lively Member
    Join Date
    Feb 2008
    Posts
    118

    Re: Resolved: When Ascii and KeyCode can't distinguish left from right, then what?

    I should also mention, that although I have taken your extra advice on board and may take that path in future projects.
    The label is invisible to the user and only used for the lblMEvent_Change event.
    I rely heavily on trial and error, mostly only succeeding in the latter.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.