Results 1 to 2 of 2

Thread: More synchronic than GetAsyncKeyState?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 1999
    Location
    Prague, Czech Republic
    Posts
    350

    Post

    Hi again!

    Some time ago, I asked for windows equivalent for dos Inkey$. I was warned about GetAsyncKeyState API function and I would like to thank you. But in Get... I must to determine if any of the key is pressed. Inkey$ always contains the pressed key - it is quicklier, and at the main point - it is more reliable: when I use Get... with very small interval, i will get the result sometimes more than 6x and when i use longer intervals, i will some keys forget. After some time with playing with it I find the correct interval, but the keys are not in correct order all the time.

    Does anybody know something more powerfull, then GetAsynKeyState? I need to capture the key even if my apliation is not active!



    ------------------
    Thanks,
    John, 14 years old

  2. #2
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Post

    The only other method is to use a Keyboard Hook, but that's only possible within you're Apps Thread unless you link it via a DLL, instead try this version of a Key Logger that I wrote using the GetAsyncKeyState API, it uses a fine Time Interval, but also prevents data repeatition.

    Add a Timer to a Form..
    Code:
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    Private Declare Function GetKeyboardState Lib "user32" (pbKeyState As Byte) As Long
    Private Declare Function ToAscii Lib "user32" (ByVal uVirtKey As Long, ByVal uScanCode As Long, lpbKeyState As Byte, lpwTransKey As Long, ByVal fuState As Long) As Long
    
    Private Const VK_SHIFT = &H10   'Used by Win9x
    Private Const VK_LSHIFT = &HA0  'Used by NT
    Private Const VK_RSHIFT = &HA1  'Used by NT
    
    Private Sub Form_Load()
        'Need a Very Low Interval to Ensure the Keys are Captured in the Correct Order
        Timer1.Interval = 10
        Timer1.Enabled = True
    End Sub
    
    Private Sub Timer1_Timer()
        Dim iKey As Integer
        Dim iAsc As Long
        Dim bKeys(255) As Byte
        
        For iKey = 1 To 255
            'Use GetAsyncKeyState to Monitor Keypress From Anywhere in the O/S.
            If GetAsyncKeyState(iKey) And iKey <> VK_LSHIFT And iKey <> VK_RSHIFT And iKey <> VK_SHIFT Then Exit For
        Next
        If iKey < 256 Then
            'Get the Current Keyboard State For the Shift Keys Etc..
            Call GetKeyboardState(bKeys(0))
            While GetAsyncKeyState(iKey)
                'Wait for Key to be Released
            Wend
            'Conver the Key to it's ASCII Equivilant
            Call ToAscii(iKey, 0&, bKeys(0), iAsc, 0&)
            If iAsc Then
                'Store Keypress to Log Here
                Debug.Print Chr(iAsc);
            End If
        End If
    End Sub

    ------------------
    Aaron Young
    Analyst Programmer
    [email protected]
    [email protected]



    [This message has been edited by Aaron Young (edited 01-10-2000).]

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