Skip to content

Instantly share code, notes, and snippets.

@GeeLaw
Created November 13, 2025 02:30
Show Gist options
  • Select an option

  • Save GeeLaw/ce7dc888b13496c6f228b7e1e319798a to your computer and use it in GitHub Desktop.

Select an option

Save GeeLaw/ce7dc888b13496c6f228b7e1e319798a to your computer and use it in GitHub Desktop.
An example usage of `SetWinEventHook` to track cursor position.
/*
Copyright 2025 Ji Luo
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef UNICODE
#define UNICODE
#endif
#ifndef UNICODE_
#define UNICODE_
#endif
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include<Windows.h>
#include<conio.h>
#include<cstdio>
#include<thread>
#include<mutex>
#include<condition_variable>
UINT const WM_USER_NotifyCursorLocationChanged = WM_USER;
int const GETCH_EscKey = 27;
std::mutex monitor_mutex;
std::condition_variable monitor_cv;
bool worker_should_exit = false;
bool cursor_location_changed = true;
VOID CALLBACK LocationChangeHook(HWINEVENTHOOK hook, DWORD event, HWND hwnd, LONG object, LONG child, DWORD thread, DWORD time)
{
if (object == OBJID_CURSOR)
{
PostMessage(nullptr, WM_USER_NotifyCursorLocationChanged, 0, 0);
}
}
void NotifyCursorLocationChanged()
{
monitor_mutex.lock();
cursor_location_changed = true;
monitor_mutex.unlock();
monitor_cv.notify_all();
}
void WorkerThread()
{
while (true)
{
do
{
std::unique_lock<std::mutex> lock{ monitor_mutex };
while (true)
{
if (worker_should_exit)
{
return;
}
if (cursor_location_changed)
{
cursor_location_changed = false;
break;
}
monitor_cv.wait(lock);
}
} while (false);
POINT pos;
if (GetCursorPos(&pos))
{
std::fprintf(stderr, "\rasync cursor position = { %d, %d } ",
(int)pos.x, (int)pos.y);
}
}
}
int main()
{
/* This is not subject to UIPI --- even if the active window and the current process
** cannot send messages to each other, the cursor location change notification
** is still delivered. */
HWINEVENTHOOK hook = SetWinEventHook(EVENT_OBJECT_LOCATIONCHANGE, EVENT_OBJECT_LOCATIONCHANGE, nullptr, LocationChangeHook, 0, 0, WINEVENT_OUTOFCONTEXT);
if (!hook)
{
return -1;
}
std::thread worker{ WorkerThread };
while (true)
{
while (_kbhit())
{
if (_getch() == GETCH_EscKey)
{
PostQuitMessage(0);
}
}
MSG msg;
BOOL ret = GetMessage(&msg, nullptr, 0, 0);
if (ret == -1 || !ret)
{
break;
}
if (msg.message == WM_USER_NotifyCursorLocationChanged)
{
NotifyCursorLocationChanged();
continue;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
monitor_mutex.lock();
worker_should_exit = true;
monitor_mutex.unlock();
monitor_cv.notify_all();
worker.join();
UnhookWinEvent(hook);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment