Solved

Key capture doesnt always work

Posted on 2013-05-25
6
472 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 85

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now