简体   繁体   中英

How to create a window focus listener in windows using win32

Is it possible to create a listener that listens every time a window losses focus in WindowsOS and then runs the callback function? I am using win32api, win32net, wmi, win32gui, win32process. I am using GetActiveWindow() to get info i need but i am doing this with a sleep timer and i was wondering maybe there is a listner to which i could attach a handler to handle every time the window focus is changed for any window in WindowsOS.

SetWinEventHook (EVENT_OBJECT_FOCUS, EVENT_OBJECT_FOCUS, 0, HandleWinEvent, 0, 0, WINEVENT_OUTOFCONTEXT )

void CALLBACK HandleWinEvent(HWINEVENTHOOK /*hook*/, DWORD event, HWND hwnd, 
                             LONG /*idObject*/, LONG /*idChild*/, 
                             DWORD dwEventThread, DWORD dwmsEventTime)
{
    if (event == EVENT_OBJECT_FOCUS )
    {
        DbgPrint("%u:%x %p\n", dwmsEventTime, dwEventThread, hwnd);
    }
}

这是在Python中使用SetWinEventHook更详细的示例: 如何在python中使用winapi SetWinEventHook?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM