Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Key capture doesnt always work

Posted on 2013-05-25
6
Medium Priority
?
499 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 14

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 46

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 46

Accepted Solution

by:
aikimark earned 2000 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying 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

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…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

886 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