4 Haziran 2018 Pazartesi

SetWindowsHookEx metodu

Giriş
Bu metodun tersini UnhookWindowsHookEx yapar.

Örnek - WH_KEYBOARD_LL
Şöyle yaparız.
LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
  PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT) lParam;
  ...
  return CallNextHookEx(NULL, nCode, wParam, lParam);
}

HOOKPROC LockerPraq()
{
  HHOOK hook_install = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc,
    NULL, 0);
  if (NULL == hook_install)
    return(0);

  MSG msg;
  while (GetMessage(&msg, NULL, 0, 0))
  {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
  }

  UnhookWindowsHookEx(hook_install);

  return(0);
}
Örnek
Elimizde şöyle bir kod olsun.
LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
  if (nCode == HC_ACTION)
  {
    auto &ms = * (const MOUSEHOOKSTRUCT *) lParam;
    if (wParam == WM_MOUSEMOVE)
    {
      cout << "X : " << ms.pt.x << " Y: " << ms.pt.y << "\n";
    }
  }
  return CallNextHookEx(hMSHook, nCode, wParam, lParam);
}
Şöyle yaparız.
int _tmain() {
  HMODULE hInstance = GetModuleHandle(NULL);
  hMSHook = SetWindowsHookEx(WH_MOUSE_LL, MouseProc, hInstance, NULL);

  MSG Msg;

  while (GetMessage(&Msg, NULL, 0, 0)) { DispatchMessage(&Msg); }
  ::ReleaseDC(0, dc);

  return 0;
}

Hiç yorum yok:

Yorum Gönder