简体   繁体   English

防止窗口焦点变化

[英]Prevent Window Focus Change

I'm trying to help a disabled person with a small bit of code to help him play a game easier. 我正在尝试用一小段代码帮助一个残疾人,以帮助他更轻松地玩游戏。 He is limited to a trackball and a single button. 他只限于一个轨迹球和一个按钮。

Currently he uses the onscreen keyboard and has managed to play other games using it. 目前他使用屏幕键盘并设法使用它玩其他游戏。 I've created a small bit of code to try and make his live easier. 我创建了一小段代码来尝试让他的生活更轻松。 It uses hover buttons for the keys. 它使用悬停按钮作为键。 When the mouse pointer enters the button it sends key down, when it moves off the button it sends key up. 当鼠标指针进入按钮时,它会向下发送按键,当它离开按钮时它会向上发送按键。

I have that working and I think (or hope) he's going to like it. 我有这个工作,我想(或希望)他会喜欢它。 It worked pretty well when I tested it. 当我测试它时,它工作得很好。

I want to add one more piece of functionality to it. 我想为它添加一个功能。 If he clicks while over my control, I want to send a different key stroke. 如果他在我的控制下点击,我想发送一个不同的击键。 Not a problem, I can do that. 没问题,我可以做到。 However, when I do that window focus shifts from the game to my control. 然而,当我这样做时,焦点从游戏转移到我的控制。

I found this on MSDN: 我在MSDN上发现了这个:

http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/48737c2b-7e6f-4ade-ac1c-7dd2f5cc2b88/ http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/48737c2b-7e6f-4ade-ac1c-7dd2f5cc2b88/

That works to prevent my window from activating, but it still causes the game to lose focus. 这可以防止我的窗口激活,但它仍然会导致游戏失去焦点。 I can set the focus back to the game, but I would prefer if it just never lost focus. 我可以将重点放回到游戏中,但我更愿意,如果它永远不会失去焦点。

I'm coding in C# and WPF. 我在C#和WPF编码。

Anyone have any ideas on how you might do that? 任何人对你如何做到这一点都有任何想法?

John Fenton 约翰芬顿

Hook in some low level mouse and keyboard hooks, and your application won't need to steal focus and give it back. 钩住一些低级别的鼠标和键盘钩子,你的应用程序不需要窃取焦点并将其还原。

Take a look at this pastie of a simple WPF app , which is heavily based off of an article by Stephen Toub . 看看这个简单的WPF应用程序的贴图 ,它基于Stephen Toub的一篇文章

Whenever you left click, anywhere, a second left click is sent, so you effectively double click. 无论何时在任何地方左键单击,都会发送第二次左键单击,因此您可以有效地双击。 You could achieve your goal by hiding your app whenever the user clicks, so that your app never steals focus. 您可以通过在用户点击时隐藏您的应用来实现您的目标,从而使您的应用永远不会窃取焦点。 Then detect the location of the click, and decide if one of your buttons would have been pressed, and act accordingly. 然后检测点击的位置,并确定是否按下了其中一个按钮,并采取相应措施。

I realize I'm digging this out of the past, but hopefully someone will find this answer helpful. 我意识到我正在挖掘过去,但希望有人会觉得这个答案很有帮助。 Cheers, and good for you for turning your programming to assist a fellow human! 干杯,对你来说有利于你的编程,以帮助人类同胞!

You could try canceling the event PreviewLostKeyboardFocus as mention in this blog: 您可以尝试取消此博客中提到的事件PreviewLostKeyboardFocus:

http://immortalratblog.blogspot.com/2008/03/canceling-keyboard-focus-change-in-wpf.html http://immortalratblog.blogspot.com/2008/03/canceling-keyboard-focus-change-in-wpf.html

Im guessing that focus is lost be simulating the key press? 我猜测焦点丢失了,模仿按键?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM