在 WebBrowser 获得焦点后获取键盘事件
我遇到过一种情况,我想要使用 WFP WebBrowser
,但是当用户按下按钮时会发生一些事情;但是,在 WebBrowser
获得焦点后,我的应用程序中不再触发某些键盘和鼠标事件。
重现:创建一个新项目,设置 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>
并让代码隐藏覆盖 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);
}
}
好的,可以理解的是,用户可能想输入他的 Google 查询。但在某些时候我想重新获得控制权。为此,我设计了一个按钮。当您单击此按钮时,我希望键盘控制返回到 WPF 应用程序。但无论按钮做什么,我都无法再次触发 OnKeyDown()
。
我的特殊限制允许在此时销毁 WebBrowser
。我尝试清除其父容器,尝试调用 Dispose() 和垃圾收集器。尝试Focus()
关注具有该功能的事物。似乎没有什么能夺回控制权。
我宁愿避免创建新的 Window() 或类似效果的解决方案。
编辑
我发现放置一个TextBox
并使其成为焦点可以让我重新获得焦点!然而,我的窗口中没有文本框,仅仅为了咯咯笑而添加一个文本框充其量似乎是违反直觉的。
编辑2
当前的临时解决方案放置了一个不可见的(嗯,有点,它只是0乘0,Visibility.Hidden
不起作用)TextBox
-启用它、聚焦它并禁用它。如果不首先禁用它,一些键将由 TextBox
处理,而不是冒泡到 KeyDown()
。
I have a situation where I would want to use WFP WebBrowser
, but when the user presses a button something happens; however after WebBrowser
gets focus, some keyboard and mouse events no longer fire in my app.
To reproduce: Create a new project, set 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.
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. 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. But no matter what the button does, I can't get OnKeyDown()
to fire again.
My particular restrictions allow WebBrowser
to be destroyed at this point. I tried clearing its parent container, tried calling Dispose()
and the garbage collector. Tried Focus()
ing on things that have that functionality. Nothing seems to get control back.
I'd rather avoid solutions which create new Window()
or something to that effect.
EDIT
I've found that putting a TextBox
and making it focus gets me back focus! However I have no textboxes in my window, and adding one just for giggles seems counter-intuitive at best.
EDIT 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. Without disabling it first some keys are handled by TextBox
instead of bubbling up to KeyDown()
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,它仅适用于
Enter
键,修复方法是使用 OnKeyUp() 作为Enter
键......Yes it is reproducible for
Enter
key only and the fix is to use OnKeyUp() forEnter
Key....