Solved

Key capture doesnt always work

Posted on 2013-05-25
6
483 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

837 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