Results 1 to 3 of 3

Thread: GetAsyncKeyState

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2000
    Posts
    50

    Unhappy

    How can I catch all keyboard presses in the system.Can I use GetAsyncKeyState function ? Or must I use SetWindowsHookEx ? Please send me source code in the answer.

  2. #2
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    To monitor all keys pressed any time (even when your form isn't active) using a GlobalHook you must put the HookProc in a DLL file which Vb can't make.C++ can. As an alternative you can use this code.
    Code:
    'in a module
    Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
    Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
    Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    
    Function GetPressedKey() As String
        For Cnt = 32 To 128 'or 32 to 256
            'Get the keystate of a specified key
            If GetAsyncKeyState(Cnt) <> 0 Then
                GetPressedKey = Chr$(Cnt)
                Exit For
            End If
        Next Cnt
    End Function
    Sub TimerProc(ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long)
        Ret = GetPressedKey
        If Ret <> sOld Then
            sOld = Ret
            sSave = sSave + sOld
        End If
    'sSave contains pressed keys
    End Sub
    In a form
    Code:
    Private Sub Form_Load()
    SetTimer Me.hwnd, 0, 1, AddressOf TimerProc
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
    KillTimer Me.hwnd, 0
    End Sub
    [Edited by Vlatko on 10-29-2000 at 03:56 PM]
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  3. #3
    Guest
    It might be a good idea to include key 13 (Return) as well.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width