Original post address
Run rupenghooker.exe in the debugdirectory of the compressed package, and then openProgramWhat do you do, click a program, enter something, and then close the program? Haha, I wrote down everything you entered !!! Well, I am a good guy and don't do bad things.
(119,686) [] (toolbarwindow32) press the left mouse button
(Enter) is pressed
(H) pressed
(E) pressed
(L) pressed
(L) pressed
(O) pressed
(181,323) [untitled-Notepad] (Notepad) press the left mouse button
(191,390) [] (#32768) press the left mouse button
(276,422) folderview (syslistview32) press the left mouse button
(276,422) folderview (syslistview32) press the left mouse button
(598,440) [Yes (& Y)] (button) press the left mouse button
(818,29720.a.txt-Notepad] (Notepad) press the left mouse button
(818,288) [computer learning for college students at www.rupeng.comCommunity] (#32770) press the left mouse button
The specific principle is Windows Hook. Use setwindowshookex (wh_mouse_ll, mousehookproc, instance, 0); set the mousehookproc function to the callback of the mouse hook, when a mouse action occurs, the mousehookproc function is called. In the mousehookproc function, the mouse action and position can be obtained:
- Lpmousehookstruct lpmousehook = (mousehookstruct far *) lparam;
- If (wm_lbuttondown = wparam)
- {
- Tchar lptext [256];
- Tchar lpclassname [256];
- Hwnd = windowfrompoint (lpmousehook-> pt );
- Getwindowtext (hwnd, lptext, sizeof (lptext ));
- Getclassname (hwnd, lpclassname, sizeof (lpclassname ));
- Fprintf (FP, "(% I, % I) [% s] (% s) press the left mouse button \ n", lpmousehook-> PT. x, lpmousehook-> PT. y, lptext, lpclassname );
- }
Here, we use windowfrompoint to get the handle of the control at the current position of the mouse, and then get the text and Class Name of the control through getwindowtext and getclassname. Haha, I will know which control you clicked.
The same is true for keyboard hooks.
The record keyboard also uses setwindowshookex (wh_keyboard_ll, keyhookproc, instance, 0); sets the keyboard hook and uses the keyhookproc function to respond to messages:
- Lpkbdllhookstruct lpkeyhook = (lpkbdllhookstruct) lparam;
- If (wm_keydown = wparam | wm_syskeydown = wparam)
- {
- Long lkey = (loword (lpkeyhook-> scancode) <16 );
- Tchar lpkeyname [256];
- Getkeynametext (lkey, lpkeyname, sizeof (lpkeyname ));
- Fprintf (FP, "(% s) is pressed \ n", lpkeyname );
- }
Because the position of lpkeyhook-> scancode represents the scan code, you must first use loword to retrieve the high position, and then shift the 16 digits to the right, finally, use getkeynametext to get the description of the key (a, B, c, F1, or enter ).
Source code download