Link to home
Start Free TrialLog in
Avatar of simonwait
simonwaitFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Key capture doesnt always work

I have a piece of code which captures keys and transfers this to output a text string to a textbox.

The code seems to work fine but it needs to work every time in quick succession and this doesn't always seem to happen with the F9/F10 where the textbox string seems to stick.  If I do nothing for 5 or so seconds it will carry on working.  Is there some form of buffer?

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

    If KeyCode = 16 Then Exit Sub

    Select Case Shift

    Case 1

        Select Case KeyCode
        Case 120
            KeyPressed = "Load"
        Case 121
            KeyPressed = "Clear"
        Case 112
            KeyPressed = "1 Started"
        Case 113
            KeyPressed = "2 Started"
        Case 114
            KeyPressed = "3 Started"
        Case 115
            KeyPressed = "4 Started"
        Case 116
            KeyPressed = "5 Started"
        Case 117
            KeyPressed = "6 Started"
        Case 118
            KeyPressed = "All Started"
        End Select

    Case 2

        Select Case KeyCode
        Case 120
            KeyPressed = "Load"
        Case 121
            KeyPressed = "Clear"
        Case 112
            KeyPressed = "1 Stopped"
        Case 113
            KeyPressed = "2 Stopped"
        Case 114
            KeyPressed = "3 Stopped"
        Case 115
            KeyPressed = "4 Stopped"
        Case 116
            KeyPressed = "5 Stopped"
        Case 117
            KeyPressed = "6 Stopped"
        Case 118
            KeyPressed = "All Stopped"
        End Select

    End Select

    lblKeyPress.Caption = KeyPressed

End Sub

Open in new window

Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

"If I do nothing for 5 or so seconds it will carry on working."

Are you holding down the key?  If so, then this is a function of the operating system.  There is a delay before the keys start to repeat:
http://www.dummies.com/how-to/content/set-your-keyboards-repeat-delay-and-repeat-rate.html
Avatar of simonwait

ASKER

Im holding down the Shift key but switching between the F9/F10 keys every 0.5 secs or so.  The same process on F1-F7 seems to not have the same problem.
Apart from that, you should avoid using (only) KeyDown event. Instead try to use KeyUp or KeyPressed...
You might try a simpler version of your code that makes fewer comparisons.  If you know that some of the keys will be pressed more often than others, you might arrange the Case clauses to put the most frequently occurring at the top of the list.
Option Explicit

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = 16 Then Exit Sub

    Select Case KeyCode
        Case 120
            lblKeyPress.Caption = "Load"

        Case 121
            lblKeyPress.Caption = "Clear"
        
        Case 112 To 117
            lblKeyPress.Caption = (KeyCode - 111) & Array("", " Started", " Stopped")(Shift)
            
        Case 118
            lblKeyPress.Caption = "All" & Array("", " Started", " Stopped")(Shift)
    End Select
    
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of aikimark
aikimark
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Great not only is that more efficient but also it seems to have resolved the original problem.  Thanks very much