Winodws HOOK keyboard HOOK

Source: Internet
Author: User

HOOK is a platform for message processing in Windows. Applications can set sub-programs to monitor certain messages or specific events (such as keyboard and mouse events) in a specified window ), the monitored window can be created by other processes. When a message arrives, process it before the target window processing function. The hook mechanism allows applications to intercept and process window messages or specific events.

The following is an example code that uses a keyboard HOOK to HOOK a keyboard. When the keyboard is captured and pressed, a sound is played.

# Include <windows. h> # include <tchar. h> # include <Mmsystem. h> # pragma comment (lib, "Winmm. lib "); HHOOK hHook; // lresult callback LowLevelKeyboardProc (int nCode, WPARAM wParam, LPARAM lParam); int APIENTRY _ tWinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) {MSG msg; // create a low-level keyboard hook hHook = SetWindowsHookEx (WH_KEYBOARD_LL, LowLevelKeyboardProc, hInstance, 0); // main message loop: while (GetMessage (& msg, NULL, 0, 0); // uninstall the low-level keyboard hook UnhookWindowsHookEx (hHook); return (int) msg. wParam;} // lresult callback (int nCode, WPARAM wParam, LPARAM lParam) {KBDLLHOOKSTRUCT * kbllHookStruct = (KBDLLHOOKSTRUCT *) lParam; // if you press the button if (wParam = WM_KEYDOWN) {// play the sound PlaySound (_ T ("shake.wav"), NULL, SND_FILENAME | SND_ASYNC );} TCHAR str [255] = {0}; _ stprintf (str, _ T ("Msg Code = 0x % x, vkCode = % d \ n"), wParam, kbllHookStruct-> vkCode); OutputDebugString (str); // send the Keyboard Message to the next hook return CallNextHookEx (hHook, nCode, wParam, lParam );}

 

 

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.