1

I wanted to add a system foreground event hook to track the active window and track mouse clicks(right-click) within the active window. I add eventHook for EVENT_SYSTEM_FOREGROUND, which is working as expected, my hook:

HWINEVENTHOOK hHook = SetWinEventHook (EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND,
    NULL, MyEventHandler, 0, 0, WINEVENT_OUTOFCONTEXT | WINEVENT_SKIPOWNPROCESS);

where MyEventHandler is

void CALLBACK MyEventHandler (
  HWINEVENTHOOK hWinEventHook,
  DWORD event,
  HWND hwnd,
  LONG idObject,
  LONG idChild,
  DWORD idEventThread,
  DWORD dwmsEventTime
)
{ ... }

How to add a mouse event handler to track mouse operations like right click?

Adam
  • 23
  • 4
  • 2
    If you're going to quote from my answer to [this question](https://stackoverflow.com/questions/68729032/how-to-record-screentime-in-c) then you might at least [accept it](https://stackoverflow.com/help/someone-answers). – Paul Sanders Aug 12 '21 at 13:02
  • Thanks @PaulSanders. Sorry I could have done this before. – Adam Aug 12 '21 at 13:08
  • No worries, thanks for doing it. I'll answer this question later, when I am not using a tablet. – Paul Sanders Aug 12 '21 at 13:19
  • `SetWinEventHook()` does not expose any mouse events. But `SetWindowsHookEx()` does, via `WH_MOUSE/_LL`. It also has `WH_CALLWNDPROC/RET` for tracking message processing on a per-window basis, like `WM_RBUTTON(DOWN|UP)`. – Remy Lebeau Aug 12 '21 at 18:58
  • You can use [raw input](https://learn.microsoft.com/en-gb/windows/win32/inputdev/raw-input) to intercept mouse messages globally. There's an answer showing how to do that [here](https://stackoverflow.com/a/41154576/5743288). – Paul Sanders Aug 12 '21 at 22:22
  • PS: This has the advantage that the hook code does not need to be placed in a DLL. – Paul Sanders Aug 13 '21 at 19:29

0 Answers0