简体   繁体   English

从其他应用程序进入时应用程序没有获得键盘焦点

[英]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.

相关问题 (VB或C#)即使应用程序未对焦,也可以在系统托盘中运行应用程序并聆听键盘输入 - (VB or C#) Run app in system tray and listen to keyboard input even when the app is not in focus c#将焦点从其他应用程序返回给父表单 - c# Giving focus back to parent form from other app 在焦点事件上打开触摸键盘窗口10 通用应用 - Open touch keyboard windows 10 on focus event TextBox | Universal App 如何将WPF应用设置为始终处于非活动状态,以使其不会从其他应用中窃取焦点 - How to set WPF app to always be inactive so it does not steal focus from other apps KeyBoard focus wpf:从其他UserControls TreeView按TAB时,将KeyBoard Focus设置为DataGrids第一行 - KeyBoard focus wpf : Set KeyBoard Focus to a DataGrids First row when Pressed TAB from a different UserControls TreeView 当应用重新聚焦时重新启动MediaCapture - Restart MediaCapture when app comes back into focus 应用启动时如何聚焦UrhoSharp窗口? - How to focus UrhoSharp window when app starts? 当PasswordBox获得焦点时,软件键盘将从屏幕上消失 - Software Keyboard Dissapears from screen when PasswordBox given focus UWP:当应用程序在后台时监听键盘事件 - UWP: listen keyboard event when app is in background Winforms:用于在面板中托管其他应用程序的Winforms应用程序的键盘快捷键 - Winforms: Keyboard shortcuts for Winforms app hosting other applications in its panels
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM