简体   繁体   English

WebBrowser获得焦点后,重新获取键盘事件

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

相关问题 带有WebBrowser控件的Excel CustomTaskPane - 键盘/焦点问题 - Excel CustomTaskPane with WebBrowser control - keyboard/focus issues 当Entry获得焦点时隐藏键盘 - Hide keyboard when Entry gets focus WPF 4键盘焦点:永远不会收到关键事件 - WPF 4 keyboard focus: never recieving key events 在 WPF 中丢失弹出窗口后,如何重新获得对弹出窗口的关注? - How to get back the focus on popup, after losing it in WPF? 在多个回发事件后将焦点设置到文本框时出现问题 - issue while setting focus to text box after multiple post back events 如何在Office自定义任务窗格中的WebBrowser控件中启用键盘事件 - How to enable keyboard events in WebBrowser control in Office custom task pane 如何在 WPF WebBrowser 中使用“后退”和“前进”导航按钮事件? - How to use 'Back' & 'Forward' navigation button events in WPF WebBrowser? 在组合框获得焦点后,将加载WPF ComboBoxItem样式 - WPF ComboBoxItem Style gets loaded after Combobox gets focus 如何拦截所有键盘事件并防止在 WinForms 应用程序中失去焦点? - How to intercept all the keyboard events and prevent losing focus in a WinForms application? wpf键盘焦点:菜单打开后失去焦点? - wpf keyboard focus: losing focus after menu opens?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM