[英]App not getting keyboard focus when entering from other app
In WPF I have a Window_KeyDown event which changes the mouse cursor shape when the user presses shift.在 WPF 中,我有一个 Window_KeyDown 事件,当用户按下 shift 时,它会改变鼠标光标的形状。 It works as expected except if I have clicked on another app.它按预期工作,除非我点击了另一个应用程序。 So I added a Window_MouseEnter event like this to grab keyboard focus when the mouse reenters my app:所以我添加了一个这样的 Window_MouseEnter 事件来在鼠标重新进入我的应用程序时抓住键盘焦点:
private void Window_MouseEnter(object sender, MouseEventArgs e)
{
IInputElement b = Keyboard.Focus(this);
this.Focus();
Debug.WriteLine(b + DateTime.Now.ToLongTimeString());
}
I can see the MouseEnter event firing (with the debug line) when the mouse enters the app but my app still doesn't get keyboard events until I click in the app.当鼠标进入应用程序时,我可以看到 MouseEnter 事件触发(带有调试行),但我的应用程序在我单击应用程序之前仍然没有获得键盘事件。
It's irritating because the mouse cursor changes properly when the mouse enters controls within my app so one would expect things to work but the shift-mouse functions don't work until after a click.这很烦人,因为当鼠标进入我的应用程序中的控件时,鼠标光标会正确更改,因此人们希望事情可以正常工作,但鼠标移动功能在单击后才起作用。
What am I missing?我错过了什么?
I found that the secret is the Activate() method.我发现秘密是 Activate() 方法。 I put it in the MouseEnter function which has a side-effect of forcing the entire app to show if some of it was hidden by other apps.我将它放在 MouseEnter 函数中,该函数具有强制整个应用程序显示其中一些是否被其他应用程序隐藏的副作用。
With Activate(), the Focus() method is not needed.使用 Activate(),不需要 Focus() 方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.