pisya

lab 4

Mar 21st, 2021 (edited)
777
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.70 KB | None | 0 0
  1. #include <fstream>
  2. #include <iostream>
  3. #include <Windows.h>
  4. #include <stdio.h>
  5.  
  6. using namespace std;
  7.  
  8. void ExitProgramm();
  9. HHOOK hMouseHook;
  10. HHOOK hKeyboardHook;
  11.  
  12. LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
  13.     PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT)lParam;
  14.  
  15.     if (p->vkCode == VK_ESCAPE) {
  16.         printf("<ESC> was pressed\n");
  17.         ExitProgramm();
  18.     }
  19.     return CallNextHookEx(NULL, nCode, wParam, lParam);
  20. }
  21.  
  22. LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam) {
  23.     MOUSEHOOKSTRUCT* pMouseStruct = (MOUSEHOOKSTRUCT*)lParam;
  24.  
  25.     if (pMouseStruct != NULL) {
  26.         switch (wParam) {
  27.             case WM_LBUTTONDOWN:  printf("Left button down\n");    break;
  28.             case WM_LBUTTONUP:    printf("Left button up\n");      break;
  29.  
  30.             case WM_RBUTTONDOWN:  printf("Right button down\n");   break;
  31.             case WM_RBUTTONUP:    printf("Right button up\n");     break;
  32.  
  33.             case WM_MOUSEMOVE:    printf("Mouse move\n");          break;
  34.             case WM_MOUSEWHEEL:   printf("Mouse wheel scroll\n");  break;
  35.             case WM_MOUSEHWHEEL:  printf("Mouse hwheel scroll\n"); break;
  36.         }
  37.     }
  38.     return CallNextHookEx(NULL, nCode, wParam, lParam);
  39. }
  40.  
  41. int main() {
  42.     hMouseHook = SetWindowsHookEx(WH_MOUSE_LL, MouseProc, NULL, NULL);
  43.     hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, NULL, NULL);
  44.     MSG msg;
  45.  
  46.     while (!GetMessage(&msg, NULL, NULL, NULL)) {
  47.         TranslateMessage(&msg);
  48.         DispatchMessage(&msg);
  49.     }
  50.  
  51.     ExitProgramm();
  52. }
  53.  
  54. void ExitProgramm() {
  55.     UnhookWindowsHookEx(hMouseHook);
  56.     UnhookWindowsHookEx(hKeyboardHook);
  57.     exit(0);
  58. }
  59.  
Advertisement
Add Comment
Please, Sign In to add comment