[英]Get Keyboard events back, after WebBrowser gets focus
I have a situation where I would want to use WFP WebBrowser
, but when the user presses a button something happens; 在某些情况下,我想使用WFP
WebBrowser
,但是当用户按下按钮时,会发生某些事情。 however after WebBrowser
gets focus, some keyboard and mouse events no longer fire in my app. 但是,在
WebBrowser
获得焦点之后,某些键盘和鼠标事件将不再在我的应用中触发。
To reproduce: Create a new project, set XAML: 重现:创建一个新项目,设置XAML:
<Window x:Class="ProblemKeyboard.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<WebBrowser x:Name="browser" Height="177" HorizontalAlignment="Left" Margin="12,12,0,0" VerticalAlignment="Top" Width="479" />
</Grid>
</Window>
and let the codebehide override OnKeyDown()
event. 并让代码隐藏重写
OnKeyDown()
事件。
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
browser.Navigate("http://www.google.com");
//The above line causes browser to focus
//and as a consequence the OnKeyDown() handler
//doesn't get called again
}
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.Key == Key.Enter) MessageBox.Show("Yey!");
base.OnKeyDown(e);
}
}
Okay, understandably the user might want to type his Google query. 好的,可以理解的是,用户可能想输入他的Google查询。 But at some point I want to get control back.
但是在某个时候我想重新获得控制权。 To this end, I've devised a button.
为此,我设计了一个按钮。 When you click this button I want keyboard control to come back to the WPF app.
当您单击此按钮时,我希望键盘控制返回到WPF应用程序。 But no matter what the button does, I can't get
OnKeyDown()
to fire again. 但是无论按钮做什么,我都无法再次触发
OnKeyDown()
。
My particular restrictions allow WebBrowser
to be destroyed at this point. 我的特殊限制允许此时销毁
WebBrowser
。 I tried clearing its parent container, tried calling Dispose()
and the garbage collector. 我尝试清除其父容器,尝试调用
Dispose()
和垃圾收集器。 Tried Focus()
ing on things that have that functionality. 尝试了
Focus()
来研究具有该功能的东西。 Nothing seems to get control back. 似乎什么也无法控制。
I'd rather avoid solutions which create new Window()
or something to that effect. 我宁愿避免使用会创建新
Window()
或类似效果的解决方案。
EDIT 编辑
I've found that putting a TextBox
and making it focus gets me back focus! 我发现放一个
TextBox
并使它成为焦点可以使我重新聚焦! However I have no textboxes in my window, and adding one just for giggles seems counter-intuitive at best. 但是,我的窗口中没有文本框,仅为咯咯地笑而添加一个文本框充其量似乎是违反直觉的。
EDIT 2 编辑2
Current temporary solution puts an invisible (well, kinda, it's just 0 by 0, Visibility.Hidden
doesn't work) TextBox
- enables it, focuses it and disables it. 当前的临时解决方案放置了一个不
Visibility.Hidden
TextBox
(嗯,有点,它只是0乘0, Visibility.Hidden
无法正常工作) TextBox
启用它,对其进行聚焦并禁用它。 Without disabling it first some keys are handled by TextBox
instead of bubbling up to KeyDown()
. 如果不先禁用它,则某些键由
TextBox
处理,而不是冒泡到KeyDown()
。
Yes it is reproducible for Enter
key only and the fix is to use OnKeyUp() for Enter
Key.... 是的,仅
Enter
键可复制,并且解决方法是将OnKeyUp()用于Enter
Key。
protected override void OnKeyUp(KeyEventArgs e)
{
if (e.Key == Key.Enter) MessageBox.Show("Hi");
base.OnKeyUp(e);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.