How Can I Monitor Which Window Currently Has Keyboard Focus

前端 未结 7 666
情话喂你
情话喂你 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 17:42

    If you're programming in .net 3.5, the Automation package olorin mentions is by far the easiest, but beware of using it in a program that itself has a UI, at least if the UI is done in WPF -- the focus tracking hooks get confused by events in its own app, and quickly lock up the UI. I sent MS a bug report on it. I have not observed the same problem using a traditional Windows Forms UI. You could, of course, put the tracking code in a separate console app and use some kind of ipc to transmit the info you need.

    The tempting alternative of using Interop to access the WH_CBT Windows Hook from C# won't work -- the only global hooks you can get at from C# are the mouse and keyboard.

提交回复
热议问题