KeyUp 事件不会触发,直到我按 ALT+Tab 离开然后返回到我的应用程序
我的 Windows 窗体表单遇到一个非常奇怪的问题。正如标题中所述,直到我使用 Alt+Tab 离开窗口然后返回之前,应用程序中根本不会触发 KeyUp 事件。在切换之前我点击应用程序中的哪个位置都没有关系;什么也没发生。
此外,如果我按 alt-tab 键离开应用程序,但单击应用程序的任务栏切换回来,那么我仍然不会收到 KeyUp 事件。单击离开,然后按 alt-tab 返回,使 KeyUp 事件再次正常运行。
似乎只有当我按 Alt-Tab 键切换到应用程序时,我才会收到 KeyUp 事件 - 但在其他情况下则不会。
KeyPreview 属性为 true
。我没有在应用程序中的任何地方进行任何其他按键处理 - 只是在 KeyUp 事件中。
我真的需要这个应用程序能够响应 KeyUp 事件,特别是在启动后 - 我不想指示我的用户按 alt-tab 键离开然后返回只是为了正确地使用键盘输入功能。有没有人见过这个问题并找到解决方法?
更新:我对此进行了更多修改,并找到了一个解决方法 - 但这绝对是一个拼凑,我不喜欢它。在表单的 OnShown
覆盖中,我添加了对 this.Focus();
的调用。我还为 this.LostFocus
添加了一个事件处理程序,它也调用 this.Focus();
。这是此应用程序中的唯一表单,因此这都不是问题 - 我只是希望我了解为什么发生这种情况。 =( 啊好吧。
我将其添加为更新而不是答案,因为,好吧,这不是答案。哎呀。
I am experiencing a very strange issue with my Windows Forms form. As stated in the title, the KeyUp event is not firing in the application at all until I alt+tab away from the window, then back. Doesn't matter where I click in the application before switching away; nothing happens.
Also if I alt-tab away from the application, but click on the app's task bar to switch back, then I still don't get KeyUp events. Clicking away, then alt-tabbing back, makes the KeyUp event function properly again.
Seems I only get KeyUp events if I Alt-Tab TO the application - but not in any other case.
The KeyPreview property is true
. I'm not doing any other key processing in the application anywhere - just in the KeyUp event.
I really need this application to be able to respond to KeyUp events, especially just after startup - I don't want to instruct my users to alt-tab away then back just to have keyboard input function properly. Has anyone seen this issue and found a workaround?
Update: I've tinkered around with this a bit more, and I found a workaround - but it's definitely a kludge and I don't like it. In the OnShown
override for the form, I added a call to this.Focus();
. I also added an event handler for this.LostFocus
that also calls this.Focus();
. This is the only form in this application, so none of that is a problem - I just wish I understood why this is happening. =( Ah well.
I added this as an update instead of an answer because, well, this isn't an answer. Bleh.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许这有帮助:
http://support.microsoft.com/kb/828964
我已经覆盖了表单的 ProcessCmdKey( ) 方法以便首先“接收”表单中的加速键。
Perhaps this helps:
http://support.microsoft.com/kb/828964
I've overridden the form's ProcessCmdKey() method in order to 'receive' the accelerator keys in the form first.
我最终放弃了使用表单事件 - 我现在正在为我想要捕获的按键序列注册一个全局 Windows 热键,而且效果很好。就我而言,热键是全局的并不重要,因为我的应用程序被设计为该系统上唯一使用的应用程序。
I ended up giving up on using form events - I'm now registering a global Windows hotkey for the key sequence I want to trap, and that's working just fine. It doesn't matter in my case that the hotkey is global, as my application is designed to be the only one in use on this system.