WinForms WebBrowser 阻止 ProcessCmdKey

发布于 2024-11-04 21:47:30 字数 605 浏览 2 评论 0原文

我有一个简单的Windows 窗体应用程序,它只不过是一个包含WebBrowserForm

我正在重写 ProcessCmdKey 方法而且效果很好。但是,虽然 WebBrowser 具有焦点,但仍会调用 ProcessCmdKey,但它不再获取关键代码。

protected override bool ProcessCmdKey(ref Message msg, Keys keyData){

    //When webbrowser has focus, only control or S are found - not both.
    if(keyData==(Keys.Control|Keys.S)){
        //Do things here.
        return true;
    }

    return false;
}

I've got a simple Windows Forms application that's nothing more than a Form that contains a WebBrowser.

I'm overriding the ProcessCmdKey method and it works fine. But, while the WebBrowser has the focus, ProcessCmdKey is still called, however, it no longer picks up key codes.

protected override bool ProcessCmdKey(ref Message msg, Keys keyData){

    //When webbrowser has focus, only control or S are found - not both.
    if(keyData==(Keys.Control|Keys.S)){
        //Do things here.
        return true;
    }

    return false;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

巨坚强 2024-11-11 21:47:30

您是否尝试覆盖 WebBroswer 的 ProcessCmdKey...我隐约记得浏览器对冒泡事件做了一些奇怪的事情...与安全有关。是的,这里是:

http://msdn .microsoft.com/en-us/library/system.windows.forms.control.processcmdkey.aspx 说:

在消息预处理期间调用此方法来处理命令键。命令键是始终优先于常规输入键的键。命令键的示例包括加速器和菜单快捷键。该方法必须返回 true 来指示它已处理命令键,或返回 false 来指示该键不是命令键。仅当控件托管在 Windows 窗体应用程序中或作为 ActiveX 控件时,才会调用此方法。

ProcessCmdKey方法首先判断控件是否有ContextMenu,如果有,则启用ContextMenu来处理命令键。如果命令键不是菜单快捷方式并且控件有父级,则该键将传递到父级的 ProcessCmdKey 方法。最终效果是命令键在控制层次结构中“冒泡”。除了用户按下的键之外,按键数据还指示与该键同时按下的修饰键(如果有)。修饰键包括 SHIFT、CTRL 和 ALT 键。

我不认为它会让您在表单级别拦截浏览器按键...我认为这些事件被 WebBrowser 控件吃掉。

干杯。基思.


编辑:

http:// msdn.microsoft.com/en-us/library/system.windows.forms.keys.aspx 说:

KeyCode 从键值中提取键代码的位掩码。
修饰符 用于从键值中提取修饰符的位掩码。

该示例包含以下行:

if(e.KeyCode != Keys.Back)

if (Control.ModifierKeys == Keys.Shift) {

所以我想您需要将该密钥稍微调整到它的组成部分中。

Did you try overriding the WebBroswer's ProcessCmdKey... I vaguely recall the browser does something funky with bubbing-up events... to do with security. Yeah, here it is:

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.processcmdkey.aspx Says:

This method is called during message preprocessing to handle command keys. Command keys are keys that always take precedence over regular input keys. Examples of command keys include accelerators and menu shortcuts. The method must return true to indicate that it has processed the command key, or false to indicate that the key is not a command key. This method is only called when the control is hosted in a Windows Forms application or as an ActiveX control.

The ProcessCmdKey method first determines whether the control has a ContextMenu, and if so, enables the ContextMenu to process the command key. If the command key is not a menu shortcut and the control has a parent, the key is passed to the parent's ProcessCmdKey method. The net effect is that command keys are "bubbled" up the control hierarchy. In addition to the key the user pressed, the key data also indicates which, if any, modifier keys were pressed at the same time as the key. Modifier keys include the SHIFT, CTRL, and ALT keys.

I don't think it'll let you intercept browser keys at the form level... I think the events are eaten by the WebBrowser control.

Cheers. Keith.


EDIT:

http://msdn.microsoft.com/en-us/library/system.windows.forms.keys.aspx says:

KeyCode The bitmask to extract a key code from a key value.
Modifiers The bitmask to extract modifiers from a key value.

and the example contains the lines:

if(e.KeyCode != Keys.Back)

if (Control.ModifierKeys == Keys.Shift) {

So I guess you need to bit-twiddle that key into it's component parts.

七月上 2024-11-11 21:47:30

不幸的是,我无法从 ProcessCmdKey + Lo/Hi 单词捕获 Ctrl+S 事件。

但我可以从 WebBrowser 文档中捕获它们:

WebBrowser browser = new WebBrowser();

...
browser.Document.Body.KeyDown += new HtmlElementEventHandler(Body_KeyDown);
...
private void Body_KeyDown(Object sender, HtmlElementEventArgs e)
{
    if(e.KeyPressedCode==83 && e.CtrlKeyPressed)
        MessageBox.Show("Give me some cookies");
}

Unfortunaley I can't catch Ctrl+S event from ProcessCmdKey + Lo/Hi words.

But I can catch them from WebBrowser document:

WebBrowser browser = new WebBrowser();

...
browser.Document.Body.KeyDown += new HtmlElementEventHandler(Body_KeyDown);
...
private void Body_KeyDown(Object sender, HtmlElementEventArgs e)
{
    if(e.KeyPressedCode==83 && e.CtrlKeyPressed)
        MessageBox.Show("Give me some cookies");
}

天涯沦落人 2024-11-11 21:47:30

就我而言,我想处理 F1 键。

使用 Web 浏览器控件的按键预览会有所帮助:

webBrowserControl.PreviewKeyDown += OnWebBrowserPreviewKeyDown;

这样做不需要重写 Web 浏览器控件的 ProcessCmdKey 。

但这种方法不适用于 Ctrl+S

In my case, I wanted to process the F1 key.

Using the key preview of the webbrowser control helped:

webBrowserControl.PreviewKeyDown += OnWebBrowserPreviewKeyDown;

Doing so didn't require overriding ProcessCmdKey of the webbrowser control.

This approach doesn't work for Ctrl+S though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文