How Can I Monitor Which Window Currently Has Keyboard Focus

前端 未结 7 679
情话喂你
情话喂你 2020-12-09 17:12

Is there a way to track which window currently has keyboard focus. I could handle WM_SETFOCUS for every window but I\'m wondering if there\'s an alternative, simpler method

7条回答
  •  生来不讨喜
    2020-12-09 18:00

    So from the way you worded the question I'm inferring that you want to have an event handler which is invoked whenever focus switches between windows. You want to be notified, rather than having to poll.

    I actually don't think calling GetFocus from OnIdle is that much of a hack - sure it's polling, but it's low-overhead polling without side effects - but if you really want to track this, Windows Hooks are probably your best choice. Specifically you can install a CBT hook (WH_CBT) and listen for the HCBT_SETFOCUS notification.

    Windows calls the WH_CBT hook with this hook code when Windows is about to set the focus to any window. In the case of thread-specific hooks, the window must belong to the thread. If the filter function returns TRUE, the focus does not change.

    You could also do with with a WH_CALLWNDPROC hook and listen for the WM_SETFOCUS message.

    Depending on whether you make it a global hook, or app-local, you can track focus across all windows on the system, or only the windows owned by your process.

提交回复
热议问题