Click to See Complete Forum and Search --> : Sample Code for Logging all keypresses?
SkettaLEE
Nov 3rd, 2001, 02:03 PM
Does anyone have a sample code for logging which keys are pressed on the computer?
Hack
Nov 3rd, 2001, 07:09 PM
You need to be more specific in your request. On the face of thisDoes anyone have a sample code for logging which keys are pressed on the computer?I'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?
Xa0z
Nov 4th, 2001, 03:03 AM
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.
Talen
Nov 4th, 2001, 08:21 AM
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.
crispin
Nov 5th, 2001, 06:34 AM
Yes it is possible from VB....look here....
http://forums.vb-world.net/showthread.php?s=&threadid=44022&highlight=crispin
da_silvy
Nov 5th, 2001, 07:48 AM
'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.php?threadid=116376&goto=newpost&highlight=setwindowshook
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.