If this works, it will be better than detecting process/window changes after ALT is released AND maybe also after buttons are pressed.
Hook into Window Events
// Set up a hook to detect when foreground window actually changes
HWINEVENTHOOK hook = SetWinEventHook(
EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND,
NULL, ForegroundWindowProc, 0, 0,
WINEVENT_OUTOFCONTEXT | WINEVENT_SKIPOWNPROCESS
);
void CALLBACK ForegroundWindowProc(HWINEVENTHOOK hWinEventHook,
DWORD event, HWND hwnd, LONG idObject, LONG idChild,
DWORD dwEventThread, DWORD dwmsEventTime) {
if (event == EVENT_SYSTEM_FOREGROUND && IsWindowVisible(hwnd)) {
// This is your actual foreground window
// Handle it here
}
}