fork download
  1. #include <Windows.h>
  2.  
  3. // variable to store the HANDLE to the hook. Don't declare it anywhere else then globally
  4. // or you will get problems since every function uses this variable.
  5. HHOOK _hook;
  6.  
  7. // This struct contains the data received by the hook callback. As you see in the callback function
  8. // it contains the thing you will need: vkCode = virtual key code. (described later)
  9. KBDLLHOOKSTRUCT kbdStruct;
  10.  
  11. void SetHook();
  12. void ReleaseHook();
  13. LRESULT __stdcall HookCallback(int nCode, WPARAM wParam, LPARAM lParam);
  14.  
  15. void SetHook()
  16. {
  17. // Set the hook and set it to use the callback function above.
  18. // WH_KEYBOARD_LL means it will set a low level keyboard hook. More information can be found on MSDN.
  19. // The last 2 parameters are NULL, 0 because the callback function is in the same thread and window as the
  20. // function that sets and releases the hook. If you create a hack you will not need the callback function
  21. // in another place then your own code file anyway. Read more about it at MSDN.
  22. if (!(_hook = SetWindowsHookEx(WH_KEYBOARD_LL, HookCallback, NULL, 0)))
  23. {
  24. // Display a messagebox that notifies the user of an error. At this point, the hook is NOT installed!
  25. MessageBoxA(NULL, "Failed to install hook!", "Error", MB_ICONERROR);
  26. }
  27. }
  28.  
  29. void ReleaseHook()
  30. {
  31. // Uninstall the hook you have set. If the hook wasn't yet installed then this function will fail ofcourse.
  32. UnhookWindowsHookEx(_hook);
  33. }
  34.  
  35. // This is the callback function. Consider it the event that is raised when, in this case,
  36. // a key is pressed.
  37. LRESULT __stdcall HookCallback(int nCode, WPARAM wParam, LPARAM lParam)
  38. {
  39. if (nCode >= 0)
  40. {
  41. // the action is valid: HC_ACTION.
  42. if (wParam == WM_KEYDOWN)
  43. {
  44. // lParam is the pointer to the struct containing the data needed, so cast and assign it to kdbStruct.
  45. kbdStruct = *((KBDLLHOOKSTRUCT*)lParam);
  46. // a key (non-system) is pressed.
  47. if (kbdStruct.vkCode == VK_F1)
  48. {
  49. // F1 is pressed!
  50. MessageBoxA(NULL, "F1 is pressed!", "key pressed", MB_ICONINFORMATION);
  51. }
  52. }
  53. }
  54.  
  55. // call the next hook in the hook chain. This is nessecary or your hook chain will break and the hook stops
  56. return CallNextHookEx(_hook, nCode, wParam, lParam);
  57. }
  58.  
  59. void main()
  60. {
  61. // Set the hook
  62. SetHook();
  63.  
  64. // Don't mind this, it is a meaningless loop to keep a console application running.
  65. // I used this to test the keyboard hook functionality. If you want to test it, keep it in ;)
  66. MSG msg;
  67. while (true)
  68. {
  69.  
  70. }
  71.  
  72. //while (GetMessage(&msg, NULL, 0, 0)) { }
  73. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:1:21: fatal error: Windows.h: No such file or directory
 #include <Windows.h>
                     ^
compilation terminated.
stdout
Standard output is empty