Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

catching keys

Posted on 1999-07-09
8
Medium Priority
?
408 Views
Last Modified: 2012-08-13
GetKeyState will tell me if a key is pressed or has been pressed. This would be perfect, except for one thing. Using GetKeyState causes a relatively LONG delay before my app gets the info that a key has been pressed. Is there a better way to catch keys? One without this delay?
0
Comment
Question by:KDivad
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 5

Author Comment

by:KDivad
ID: 1523050
Edited text of question.
0
 
LVL 6

Expert Comment

by:simonbennett
ID: 1523051
Try the keypress event. You can interrogate the Keyascii argument to determine which key. And it's quick. If you want this to be done on a form level, don't forget to set the form's keypreview property to true, otherwise it will be evaluated at control level.

HTH
0
 
LVL 5

Author Comment

by:KDivad
ID: 1523052
Why would I use GetKeyState to check for keys in my own app? I appreciate the answer, but I need an alternative to GetKeyState to catch keys when my app doesn't have the focus.
0
Technology Partners: 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 10

Expert Comment

by:viktornet
ID: 1523053
When a key has been pressed 128 is being added to its scan code..

so if you wanna check whether or not the ENTER key (13) has been pressed you can check for it by doing rth3e following....

if key=141 then
  MsgBox "the ENTER key is pressed"
else
  MsgBox "the ENTER key is up"
end if

..-=ViKtOr=-..
0
 
LVL 5

Author Comment

by:KDivad
ID: 1523054
Actually, -128 is added to the code. It is 1 or 0 when the key is up, and -127 or -128 when it is down. What I need, though, is a method for catching keys that doesn't use GetKeyState, because GetKeyState has a 5 to 10 second delay when my app doesn't have the focus.
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1523055
what i know is that 128 is being added, so the ENTER should be 141 when it's down...
0
 
LVL 1

Accepted Solution

by:
kermit007 earned 280 total points
ID: 1523056
Hello KDivad

I used GetKeyState and GetAsyncKeyState in my program to catch all the key clicks and for my program it works fine... no 5 to 10 seconds delays.. here is my code, see if it helps ...  the following code records all the keystrokes even when your app has the focus or not... hope this helps...

      ____________________________________________________________________

      Create a new project.  Add 2 Timer controls (Timer1 & Timer2) and 2 TextBox controls (Text1 & Text2)
      to the new form.  Set the MultiLine property of both TextBox's to TRUE and make them pretty big so you
      can see what's going on.  Set the TabIndex of Text2 to "0" to give it focus when the form starts up.

      Add the following code :
      ____________________________________________________________________

      Option Explicit

      Private OldKey As String

      Private Sub Timer1_Timer()
         
        Dim KeyPressed As String
        Dim Counter As Integer
         
        For Counter = 1 To 255
          If GetAsyncKeyState(Counter) <> 0 Then
            ' Check if key has been pressed
            KeyPressed = CheckKey(Counter)
            ' If key has been pressed, check if it's a repeat of
            ' the last key that was pressed.  If it's a repeat,
            ' skip it
            If KeyPressed <> OldKey And KeyPressed <> "" Then
              ' Display the new key
              Text1.Text = Text1.Text & KeyPressed
              OldKey = KeyPressed
            End If
          End If
        Next
         
      End Sub

      Private Sub Timer2_Timer()
         
        ' Every 0.5 seconds, clear the old key so if you
        ' hold a key down, it will repeat twice every second.
        ' The interval of Timer2 will control how easy it is
        ' to repeat a key if held down.
        OldKey = "" 
         
      End Sub

      Private Sub Form_Load()
         
        ' The smaller the number, the faster it checks keys
        Timer1.Interval = 10
         
        ' Set TextBoxes to blank
        Text1.Text = "" 
        Text2.Text = "" 
         
        ' Set the reset speed
        Timer2.Interval = 500
         
      End Sub


      ____________________________________________________________________

      Add a Module to the project and insert the following code:
      ____________________________________________________________________

      Option Explicit

      '------------- Public Declarations ---------------
      Public Declare Function GetAsyncKeyState Lib "user32" (ByVal KEYey As Long) As Integer
      Public Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer

      '------------- Public Constants ---------------
      ' <ALT> Keys
      Public Const KEY_MENU = &H12
      Public Const KEY_LMENU = &HA4
      Public Const KEY_RMENU = &HA5
      ' <SHIFT> Keys
      Public Const KEY_SHIFT = &H10
      Public Const KEY_LSHIFT = &HA0
      Public Const KEY_RSHIFT = &HA1
      ' <CTRL> Keys
      Public Const KEY_CONTROL = &H11
      Public Const KEY_LCONTROL = &HA2
      Public Const KEY_RCONTROL = &HA3
      ' Mouse Buttons
      Public Const KEY_LBUTTON = &H1
      Public Const KEY_MBUTTON = &H4
      Public Const KEY_RBUTTON = &H2
      ' Number Pad Keys
      Public Const KEY_NUMPAD0 = &H60
      Public Const KEY_NUMPAD1 = &H61
      Public Const KEY_NUMPAD2 = &H62
      Public Const KEY_NUMPAD3 = &H63
      Public Const KEY_NUMPAD4 = &H64
      Public Const KEY_NUMPAD5 = &H65
      Public Const KEY_NUMPAD6 = &H66
      Public Const KEY_NUMPAD7 = &H67
      Public Const KEY_NUMPAD8 = &H68
      Public Const KEY_NUMPAD9 = &H69
      ' Function Keys
      Public Const KEY_F1 = &H70
      Public Const KEY_F2 = &H71
      Public Const KEY_F3 = &H72
      Public Const KEY_F4 = &H73
      Public Const KEY_F5 = &H74
      Public Const KEY_F6 = &H75
      Public Const KEY_F7 = &H76
      Public Const KEY_F8 = &H77
      Public Const KEY_F9 = &H78
      Public Const KEY_F10 = &H79
      Public Const KEY_F11 = &H7A
      Public Const KEY_F12 = &H7B
      Public Const KEY_F13 = &H7C
      Public Const KEY_F14 = &H7D
      Public Const KEY_F15 = &H7E
      Public Const KEY_F16 = &H7F
      Public Const KEY_F17 = &H80
      Public Const KEY_F18 = &H81
      Public Const KEY_F19 = &H82
      Public Const KEY_F20 = &H83
      Public Const KEY_F21 = &H84
      Public Const KEY_F22 = &H85
      Public Const KEY_F23 = &H86
      Public Const KEY_F24 = &H87
      ' Other Keys
      Public Const KEY_ADD = &H6B
      Public Const KEY_ATTN = &HF6
      Public Const KEY_BACK = &H8
      Public Const KEY_CANCEL = &H3
      Public Const KEY_CAPITAL = &H14
      Public Const KEY_CLEAR = &HC
      Public Const KEY_CRSEL = &HF7
      Public Const KEY_DECIMAL = &H6E
      Public Const KEY_DELETE = &H2E
      Public Const KEY_DIVIDE = &H6F
      Public Const KEY_DOWN = &H28
      Public Const KEY_END = &H23
      Public Const KEY_EREOF = &HF9
      Public Const KEY_ESCAPE = &H1B
      Public Const KEY_EXECUTE = &H2B
      Public Const KEY_EXSEL = &HF8
      Public Const KEY_HELP = &H2F
      Public Const KEY_HOME = &H24
      Public Const KEY_INSERT = &H2D
      Public Const KEY_LEFT = &H25
      Public Const KEY_MULTIPLY = &H6A
      Public Const KEY_NEXT = &H22
      Public Const KEY_NONAME = &HFC
      Public Const KEY_NUMLOCK = &H90
      Public Const KEY_OEM_CLEAR = &HFE
      Public Const KEY_PA1 = &HFD
      Public Const KEY_PAUSE = &H13
      Public Const KEY_PLAY = &HFA
      Public Const KEY_PRINT = &H2A
      Public Const KEY_PRIOR = &H21
      Public Const KEY_PROCESSKEY = &HE5
      Public Const KEY_RETURN = &HD
      Public Const KEY_RIGHT = &H27
      Public Const KEY_SCROLL = &H91
      Public Const KEY_SELECT = &H29
      Public Const KEY_SEPARATOR = &H6C
      Public Const KEY_SNAPSHOT = &H2C
      Public Const KEY_SPACE = &H20
      Public Const KEY_SUBTRACT = &H6D
      Public Const KEY_TAB = &H9
      Public Const KEY_UP = &H26
      Public Const KEY_ZOOM = &HFB

      ' This function takes the Virtual Key that was pressed
      ' and proccesses it so that it returns what was pressed
      ' on the keyboard
      Public Function CheckKey(nKey As Integer) As String
      On Error Resume Next
         
        Dim MidValue As String
        Dim StartPoint As Integer
         
        ' Display the Virtual Key number in the Debug window
        Debug.Print CStr(nKey)
         
        ' Test if the key pressed is a key on the keyboard that
        ' if SHIFT is held, will change
        If (nKey >= 65 And nKey <= 90) Or (nKey >= 48 And nKey <= 57) Or _
        (nKey >= 186 And nKey <= 192) Or (nKey >= 219 And nKey <= 222) Or _
        nKey = 32 Then
           
          ' Check if ALT key is pressed
          If GetKeyState(18) < 0 Then
            CheckKey = "ALT+"
          End If
           
          ' Check if CTRL key is pressed
          If GetKeyState(17) < 0 Then
            CheckKey = CheckKey + "CTRL+"
          End If
           
          ' Check if SHIFT key is pressed and if it is, check if
          ' character that's being pressed is an Alphabetic character.
          ' If it is Alphabetic, change it to CAPITAL.
          If GetAsyncKeyState(16) < 0 Then
            If nKey >= 65 And nKey <= 90 Then
              If (GetKeyState(KEY_CAPITAL) And 1) = 1 Then
                nKey = nKey + 32
              End If
            Else
              CheckKey = CheckKey + "SHIFT+"
            End If
          Else
            If nKey >= 65 And nKey <= 90 Then
              If (GetKeyState(KEY_CAPITAL) And 1) = 0 Then
                nKey = nKey + 32
              End If
            End If
          End If
           
          ' Change nKey to a character if Alpha-Numberic using the Chr()
          ' function... if not Alpha-Numeric, it returns odd character
          CheckKey = IIf(CheckKey = "", "", "[") & CheckKey & Chr(nKey) & IIf(CheckKey = "", "", "]")
           
          ' Check for SHIFT + Key and change it to the right
          ' keyboard key if one is found
          If Left(CheckKey, 7) = "[SHIFT+" Then
            Select Case CheckKey
              Case "[SHIFT+1]"
                CheckKey = "!"
              Case "[SHIFT+2]"
                CheckKey = "@"
              Case "[SHIFT+3]"
                CheckKey = "#"
              Case "[SHIFT+4]"
                CheckKey = "$"
              Case "[SHIFT+5]"
                CheckKey = "%"
              Case "[SHIFT+6]"
                CheckKey = "^"
              Case "[SHIFT+7]"
                CheckKey = "&" 
              Case "[SHIFT+8]"
                CheckKey = "*"
              Case "[SHIFT+9]"
                CheckKey = "("
              Case "[SHIFT+0]"
                CheckKey = ")"
               
              Case "[SHIFT+@]"
                CheckKey = "~"
              Case "[SHIFT+=]"
                CheckKey = "_"
              Case "[SHIFT+;]"
                CheckKey = "+"
              Case "[SHIFT+\]"
                CheckKey = "|"
              Case "[SHIFT+]]"
                CheckKey = "}"
              Case "[SHIFT+[]"
                CheckKey = "{"
              Case "[SHIFT+^]"
                CheckKey = Chr(34)
              Case "[SHIFT+:]"
                CheckKey = ":"
              Case "[SHIFT+?]"
                CheckKey = "?"
              Case "[SHIFT+>]"
                CheckKey = ">" 
              Case "[SHIFT+<]"
                CheckKey = "<" 
            End Select
          End If
           
          ' If returned odd character, change back to keyboard character
          StartPoint = InStr(CheckKey, "@")
          If StartPoint > 0 Then
            Mid(CheckKey, StartPoint, 2) = "`"
          End If
          StartPoint = InStr(CheckKey, "=")
          If StartPoint > 0 Then
            Mid(CheckKey, StartPoint, 2) = "-"
          End If
          StartPoint = InStr(CheckKey, ";")
          If StartPoint > 0 Then
            Mid(CheckKey, StartPoint, 2) = "="
          End If
          StartPoint = InStr(CheckKey, "\")
          If StartPoint > 0 Then
            Mid(CheckKey, StartPoint, 2) = "\"
          End If
          StartPoint = InStr(CheckKey, "]")
          If StartPoint > 0 Then
            Mid(CheckKey, StartPoint, 2) = "]"
          End If
          StartPoint = InStr(CheckKey, "[")
          If StartPoint > 0 Then
            Mid(CheckKey, StartPoint, 2) = "["
          End If
          StartPoint = InStr(CheckKey, "^")
          If StartPoint > 0 Then
            Mid(CheckKey, StartPoint, 2) = "'"
          End If
          StartPoint = InStr(CheckKey, ":")
          If StartPoint > 0 Then
            Mid(CheckKey, StartPoint, 2) = ";"
          End If
          StartPoint = InStr(CheckKey, "?")
          If StartPoint > 0 Then
            Mid(CheckKey, StartPoint, 2) = "/"
          End If
          StartPoint = InStr(CheckKey, ">")
          If StartPoint > 0 Then
            Mid(CheckKey, StartPoint, 2) = "."
          End If
          StartPoint = InStr(CheckKey, "<")
          If StartPoint > 0 Then
            Mid(CheckKey, StartPoint, 2) = ","
          End If
               
        ElseIf nKey >= KEY_NUMPAD0 And nKey <= KEY_NUMPAD9 Then
          CheckKey = Chr(nKey - KEY_NUMPAD0 + 48)
        ElseIf nKey >= KEY_F1 And nKey <= KEY_F24 Then
          CheckKey = "[F" & CStr(nKey - KEY_F1 + 1) & "]"
        ElseIf nKey = KEY_ESCAPE Then
          CheckKey = "[ESC]"
        ElseIf nKey = KEY_END Then
          CheckKey = "[END]"
        ElseIf nKey = KEY_HOME Then
          CheckKey = "[HOME]"
        ElseIf nKey = KEY_LEFT Then
          CheckKey = "[Left]"
        ElseIf nKey = KEY_UP Then
          CheckKey = "[Up]"
        ElseIf nKey = KEY_RIGHT Then
          CheckKey = "[Right]"
        ElseIf nKey = KEY_DOWN Then
          CheckKey = "[Down]"
        ElseIf nKey = KEY_PRIOR Then
          CheckKey = "[PageUp]"
        ElseIf nKey = KEY_NEXT Then
          CheckKey = "[PageDown]"
        ElseIf nKey = KEY_INSERT Then
          CheckKey = "[INS]"
        ElseIf nKey = KEY_DELETE Then
          CheckKey = "[DEL]"
        ElseIf nKey = KEY_RETURN Then
          CheckKey = "[ENTER]"
        ElseIf nKey = KEY_BACK Then
          CheckKey = "[Backspace]"
        ElseIf nKey = KEY_TAB Then
          CheckKey = "[TAB]"
        ElseIf nKey <> 16 And nKey <> 17 And nKey <> 18 Then
          CheckKey = "[" & CStr(nKey) & "]"
        End If
         
        Select Case CheckKey
          Case "[1]"
            CheckKey = "[LeftMouse]"
          Case "[2]"
            CheckKey = "[RightMouse]"
          Case "[4]"
            CheckKey = "[CenterMouse]"
          Case "[145]"
            CheckKey = "[ScrLock]"
          Case "[144]"
            CheckKey = "[NumLock]"
          Case "[20]"
            CheckKey = "[CapLock]"
          Case "[44]"
            CheckKey = "[PrtScn]"
          Case "[19]"
            CheckKey = "[Pause]"
          Case "[93]"
            CheckKey = "[Menu Key]"
          Case "[92]", "[91]"
            CheckKey = "[Win95 Key]"
          Case "[12]"
            CheckKey = "[Center]"
          Case "[111]"
            CheckKey = "/"
          Case "[106]"
            CheckKey = "*"
          Case "[109]"
            CheckKey = "-"
          Case "[107]"
            CheckKey = "+"
          Case "[110]"
            CheckKey = "."
        End Select

      End Function
0
 
LVL 5

Author Comment

by:KDivad
ID: 1523057
Odd, it appears to function identically to the code that I already have. Except that it doesn't have the delay. I don't know why this comes through immediately and mine didn't. I will go ahead and award you the points. Maybe I can compare and figure out the dif? I dunno. Oh, well, thanks!
0

Featured Post

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.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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…
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…
Suggested Courses

705 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