Does anyone have a sample code for logging which keys are pressed on the computer?
Printable View
Does anyone have a sample code for logging which keys are pressed on the computer?
You need to be more specific in your request. On the face of thisI'm thinking that you want code to write a program that will capture every key pressed, in any program (i.e., Word, Excel, Internet Explorer, all games, in short, any software installed on the machine, or that ever will be installed on the machine). I suspect that you have a more specific intent. What exactly do you wish to do, and in what/or where would you like to do it?Quote:
Does anyone have a sample code for logging which keys are pressed on the computer?
I've done it before by searching for the window with focus, then subclass it (don't forget to send the messages you get to the window too :D). You will now receive all keypresses for that window. If you check when it loses focus you can find the window that haves focus now, subclass it....
But a second way could be checking the getkeyboardstate API function. put it in a timer or something (not my favorite way).
This ain't the best way to do it, you are probebly looking for a event that passes the keycode or something. I don't know one, but if someone doese, it'll be greatly appreceited by me too.
You are looking for so-called 'system wide hook'. It's is not possible from VB, but you can use DLL written in C or ASM. Look on the net for one.
Yes it is possible from VB....look here....
http://forums.vb-world.net/showthrea...hlight=crispin
VB Code:
'in form Private Sub Form_Load() Hook End Sub Private Sub Form_Unload(Cancel As Integer) UnHook End Sub 'in module Public 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 Public Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long Public Const WH_KEYBOARD = 2 Dim lpPrevProc As Long Sub Hook() lpPrevProc = SetWindowsHookEx(WH_KEYBOARD, AddressOf KeyboardProc, 0, 0) End Sub Sub UnHook() UnhookWindowsHookEx lpPrevProc End Sub Function KeyboardProc(ByVal code As Long, ByVal wParam As Long, ByVal lParam As Long) As Long Debug.Print code & ":" & wParam & ":" & lParam End Function
That's as much as I have written for my little proggie
Have a look here:
http://www.vbforums.com/showthread.p...setwindowshook