Reputation: 111
I have created simple program that can capture key pressing globally on windows. i have used GetAsyncKeyState()
. this is my code.
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vkey As Integer) As Short
For i = 65 To 128
If GetAsyncKeyState(i) = -32767 Then
return Chr(i)
End If
Next i
It works properly. but if i run another application used GetAsyncKeyState()
, together my program (or two instance of my program) key capturing is does not work correctly. it capture some of keys, not all.
for example; if I typed "stackoverflow" it capture "sakvrow"
where is the poblem and how can I fix it.
Upvotes: 4
Views: 14320
Reputation: 7620
Q: where is the poblem?
The problem is explained in the documentation:
Although the least significant bit of the return value indicates whether the key has been pressed since the last query, due to the pre-emptive multitasking nature of Windows, another application can call GetAsyncKeyState and receive the "recently pressed" bit instead of your application. The behavior of the least significant bit of the return value is retained strictly for compatibility with 16-bit Windows applications (which are non-preemptive) and should not be relied upon.
Q:how can I fix it?
As Hans suggested in comments, if you want some sort of "keylogger", you should use a keybord hook. See WH_KEYBOARD_LL
Upvotes: 2
Reputation: 1182
put a textbox
to see the events
you have to add a timer
set interval
to 100 enable it put this code on it
Dim result As Integer
For i = 1 To 255
result = 0
result = GetAsyncKeyState(i)
If result = -32767 Then
TextBox1.Text = TextBox1.Text + Chr(i)
End If
Next i
Upvotes: 0