Solved

Key capture doesnt always work

Posted on 2013-05-25
6
486 Views
Last Modified: 2013-05-30
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

0
Comment
Question by:simonwait
6 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 39197105
"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
0
 
LVL 1

Author Comment

by:simonwait
ID: 39197483
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.
0
 
LVL 13

Expert Comment

by:Alexander Eßer [Alex140181]
ID: 39197563
Apart from that, you should avoid using (only) KeyDown event. Instead try to use KeyUp or KeyPressed...
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 45

Expert Comment

by:aikimark
ID: 39202161
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

0
 
LVL 45

Accepted Solution

by:
aikimark earned 500 total points
ID: 39202177
I've simplified my code to reduce the number of Case clauses.
Option Explicit

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

    Select Case KeyCode
        Case 120 To 121
            lblKeyPress.Caption = Array("Load", "Clear")(KeyCode - 120)

        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

0
 
LVL 1

Author Closing Comment

by:simonwait
ID: 39210019
Great not only is that more efficient but also it seems to have resolved the original problem.  Thanks very much
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MS Date Picker 64 bit 32 bit issue 12 64
MS Access 03, TransferText, decimal places 8 85
Export Data to Different .csv Files 26 121
Automatic Email Reminder 4 75
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question