phyderous
asked on
Capture Key Stroke in vb
Hi,
I need to capture all key strokes in the keyboard with a backround process written in vb
any ideas?
I need to capture all key strokes in the keyboard with a backround process written in vb
any ideas?
In the form_keydown event you can capture the keyascii.
In the form_keypress event you can capture the keyascii.
Sorry you need to use the keyboardhook.
here is the code
'In a module
Public Const WH_KEYBOARD = 2
Public Const VK_SHIFT = &H10
Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal ncode As Long, ByVal wParam As Long, lParam As Any) As Long
Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Public hHook As Long
Public Function KeyboardProc(ByVal idHook As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
'if idHook is less than zero, no further processing is required
If idHook < 0 Then
'call the next hook
KeyboardProc = CallNextHookEx(hHook, idHook, wParam, ByVal lParam)
Else
'check if SHIFT-S is pressed
If (GetKeyState(VK_SHIFT) And &HF0000000) And wParam = Asc("S") Then
'show the result
Form1.Print "Shift-S pressed ..."
End If
'call the next hook
KeyboardProc = CallNextHookEx(hHook, idHook, wParam, ByVal lParam)
End If
End Function
'In a form, called Form1
Private Sub Form_Load()
'KPD-Team 2000
'URL: http://www.allapi.net/
'E-Mail: KPDTeam@Allapi.net
'set a keyboard hook
hHook = SetWindowsHookEx(WH_KEYBOA RD, AddressOf KeyboardProc, App.hInstance, App.ThreadID)
End Sub
Private Sub Form_Unload(Cancel As Integer)
'remove the windows-hook
UnhookWindowsHookEx hHook
End Sub
here is the code
'In a module
Public Const WH_KEYBOARD = 2
Public Const VK_SHIFT = &H10
Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal ncode As Long, ByVal wParam As Long, lParam As Any) As Long
Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Public hHook As Long
Public Function KeyboardProc(ByVal idHook As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
'if idHook is less than zero, no further processing is required
If idHook < 0 Then
'call the next hook
KeyboardProc = CallNextHookEx(hHook, idHook, wParam, ByVal lParam)
Else
'check if SHIFT-S is pressed
If (GetKeyState(VK_SHIFT) And &HF0000000) And wParam = Asc("S") Then
'show the result
Form1.Print "Shift-S pressed ..."
End If
'call the next hook
KeyboardProc = CallNextHookEx(hHook, idHook, wParam, ByVal lParam)
End If
End Function
'In a form, called Form1
Private Sub Form_Load()
'KPD-Team 2000
'URL: http://www.allapi.net/
'E-Mail: KPDTeam@Allapi.net
'set a keyboard hook
hHook = SetWindowsHookEx(WH_KEYBOA
End Sub
Private Sub Form_Unload(Cancel As Integer)
'remove the windows-hook
UnhookWindowsHookEx hHook
End Sub
ASKER
gafoorgk,
I d/l the tlb now what ... I need some code sample ...
rajaamirapu,
Your code only work if the form is active ....
I d/l the tlb now what ... I need some code sample ...
rajaamirapu,
Your code only work if the form is active ....
Is it jsut me or does the 'WinSubHook' search not do anything?
as i told u, u can find the sample source code in www.planetsourcode.com/vb
i'm sorry for the search string i gave u. use 'subclass thunk' as search string. u'll get the sample. i've not seen a better one than this.
i'm sorry for the search string i gave u. use 'subclass thunk' as search string. u'll get the sample. i've not seen a better one than this.
lol, ok cheers
ASKER
gafoorgk,
Thanks for the new info, but this is not what I want
the message arrived only when the form is active.
I need to monitor the message all the time like we did in the good old asm.
in asm I used to hook to the keyboard inturrept and get each key stroke event if it was made in a diffrent program
the problem is I know how to do this in DOS I don't have a clue how to do it in windows.
what I need for example is
I am typing somthing in notepad I want the vb to run in the background capturing all what I wrote in notepad.
Thanks,
Thanks for the new info, but this is not what I want
the message arrived only when the form is active.
I need to monitor the message all the time like we did in the good old asm.
in asm I used to hook to the keyboard inturrept and get each key stroke event if it was made in a diffrent program
the problem is I know how to do this in DOS I don't have a clue how to do it in windows.
what I need for example is
I am typing somthing in notepad I want the vb to run in the background capturing all what I wrote in notepad.
Thanks,
ASKER
I was hoping for a better way than what spy++ is doing
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Here's a simple way of capturing all keystrokes, you can modify it to suit which keys you want to capture.
Put at timer (called Timer1) on your form.
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
'string to store all captured keystrokes
Dim str As String
Private Sub Form_Load()
make it not visible it task bar.
'App.TaskVisible = False
Timer1.Interval = 1
End Sub
Private Sub Timer1_Timer()
For i = 2 To 255
result = 0
result = GetAsyncKeyState(i)
If result = -32767 Then
str = str & Chr(i)
End If
Next i
End Sub
The string str now contains all the captured keystrokes. Notice however, that many of the keystrokes captured you might not want. For instance if you only want to capture the letters of the alphabet change For i = 2 to 255 to For i = 65 To 90. (65-90 is the ascii for A-Z).
Hope this helps :)
Put at timer (called Timer1) on your form.
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
'string to store all captured keystrokes
Dim str As String
Private Sub Form_Load()
make it not visible it task bar.
'App.TaskVisible = False
Timer1.Interval = 1
End Sub
Private Sub Timer1_Timer()
For i = 2 To 255
result = 0
result = GetAsyncKeyState(i)
If result = -32767 Then
str = str & Chr(i)
End If
Next i
End Sub
The string str now contains all the captured keystrokes. Notice however, that many of the keystrokes captured you might not want. For instance if you only want to capture the letters of the alphabet change For i = 2 to 255 to For i = 65 To 90. (65-90 is the ascii for A-Z).
Hope this helps :)
Step:
Create a new form with a timer, and a textbox. named: timer1, text1
Set timer1 interval to 1 and enabled
Then add this code to your form:
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Dim result As Integer
Private Sub Timer1_Timer()
For i = 1 To 255
result = 0
result = GetAsyncKeyState(i)
If result = -32767 Then
Text1.Text = Text1.Text + Chr(i)
End if
Next i
End Sub
DeMo:
Try pressing the keyboard while you open others window, the pressed key will appear on the textbox,
Thanks,
~ Fantasy ~
Create a new form with a timer, and a textbox. named: timer1, text1
Set timer1 interval to 1 and enabled
Then add this code to your form:
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Dim result As Integer
Private Sub Timer1_Timer()
For i = 1 To 255
result = 0
result = GetAsyncKeyState(i)
If result = -32767 Then
Text1.Text = Text1.Text + Chr(i)
End if
Next i
End Sub
DeMo:
Try pressing the keyboard while you open others window, the pressed key will appear on the textbox,
Thanks,
~ Fantasy ~
if u experiment with that code, u can see that the form can be removed and it can be done in a startup module. and u can even start that application when windows starts.
or
there is a better class and type library in www.planetsourcecode.com/vb. go there and search for 'WinSubHook'