How do i - without using an OCX - monitor all key presses in windows so my program can 'jump into action' when a certain key combo is pressed for example??
Is this easy?
Printable View
How do i - without using an OCX - monitor all key presses in windows so my program can 'jump into action' when a certain key combo is pressed for example??
Is this easy?
No need to subclass when GetAsyncKeyState is easier.
Code:Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Sub Form_Load()
Timer1.Interval = 1
End Sub
Private Sub Timer1_Timer()
If GetAsyncKeyState(vbKeyReturn) Then MsgBox ("You pressed Enter")
End Sub
This is an interesting idea, but here's another question:
Is there a way to capture all keys pressed in windows?
I want to make an app with configurable hot keys (several of them) that will work from anywhere in windows.
Also, won't the timer loop cause a slight bit of lag on slower machines?
What I'm looking for is like a KeyDown event that works from anywhere in windows.
(and NO, i'm NOT trying to make a key logger! :)
I don't think the Timer will be a problem, seeing how many people have faster machines these days. I tested this on a Pentium 100 and all was fine.
I have often wondered without success how to "hook" the keyboard, so does anyone know how to do this? Yes I am aware of the threat of keyloggers etc. and that by knowing how to hook the keyboard you are creating a keylogger, but how do they work? Please don't flame me, for potentially being a 'hacker' or something I am just interested
well I made a little keylogger once,
I used GetAsyncKeyState, with a for loop and a timer,
basicly this was it
it works OK, but the text gets a little messed up another problem is when you hold down the shift key, a bunch of non-supported characters appear(those little black bars)Code:Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Sub Form_Load()
Timer1.Enabled = True
Timer1.Interval = 100
End Sub
Private Sub Timer1_Timer()
For i = 0 To 255
If GetAsyncKeyState(i) <> 0 Then
Text1 = Text1 & LCase(Chr(i))
End If
Next
End Sub
you could filter this out, but I didnt really want to because it was just a small experiment.
[Edited by denniswrenn on 08-09-2000 at 02:04 PM]
Dennis: Your keylogger will detect a whole lot of jargon if you use all of the keys. I suggest you ignore the keys before 32.
While we're on the subject of key capturing,
How do I detect if the shift, ctrl, alt, etc. keys were pressed while a specific key was pressed?
I know that a keydown event on a form can detect the shift, ctrl, and alt keys along with the actual pressing of any key at any time. A form's keydown event could theoretically be a key logger, however it would not capture if it was not the active window. (unless there's some way to make the form a system service??) :)
The timer and for/next loop that was suggested does seem to work on new machines (considering the typing speed of most computer users), however if I was to run a program like that on my old 486/33 with 8megs RAM (just barely running Win95) some keys might perhaps be missed?
It's too bad the API doesn't have events as well as methods that can be called. :)
Thanks
Here's one method for getting about as close to a KeyLogger as you can without using a DLL to subclass the Keyboard Handler: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()
Dim iKey As Integer
'First flush the Buffer of any residual key presses
For iKey = 0 To 255
While GetAsyncKeyState(iKey)
Wend
Next
'Need a Very Low Interval to Ensure the Keys are Captured in the Correct Order
Timer1.Interval = 1
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
So using the getasynckeystate function, is there a way to figure out if the SHIFT, CTRL, ALT keys etc. have been pressed aswell as another key.
like CTRL + 6
or CTRL + ALT + F7
thanks to anyone who can help
Please can someone help. thankyou
Try this:
Code:Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Const VK_MENU = &H12
Const VK_CONTROL = &H11
Const VK_SHIFT = &H10
Private Sub Timer1_Timer()
For I = 3 To 255
If I = 16 Or I = 17 Or I = 18 Then GoTo EX:
If GetAsyncKeyState(I) And GetAsyncKeyState(VK_SHIFT) Then Print Chr(I) & "+SHIFT"
If GetAsyncKeyState(I) And GetAsyncKeyState(VK_CONTROL) Then Print Chr(I) & "+CTRL"
If GetAsyncKeyState(I) And GetAsyncKeyState(VK_MENU) Then Print Chr(I) & "+ALT"
If GetAsyncKeyState(I) Then Print Chr(I)
EX:
Next I
End Sub
Hi,
I need a similar thing.
I want to catch the use of the keycombination Ctrl+c.
The thing is, I want to do this during a lengtly operation
(copy of a large file for example).
I tried the GetAsyncKeyState api but it seems not to detect the use of the keys. I think this is because the form doesn't have focus.
Can anyone help me?
This should work regardless of whether the Form has the focus or now. Insert it into a Form with a Timer (set the Timer's interval to 1).
Code:Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Sub Timer1_Timer()
If GetAsyncKeyState(vbKeyC) And GetAsyncKeyState(vbKeyControl) Then
Open "C:\MyLog.txt" For Append As #1
Print #1, "CTRL+C WAS PRESSED AT" & Now
Close #1
End If
End Sub