且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使热键触发Windows窗体按钮?

更新时间:2023-12-06 16:45:40

假定这是一个带有WebBrowser控件的Windows Forms项目:WebBrowser会在有焦点的任何时候吃掉击键",即使Form KeyPreview也是如此.设置为"true".

Assuming this is a Windows Forms project with a WebBrowser control: the WebBrowser will "eat the keystrokes" anytime it has focus, even if Form KeyPreview is set to 'true'.

使用WebBrowser PreviewKeyDown事件调用按钮单击:

Use the WebBrowser PreviewKeyDown event to call the button click:

    private void webBrowser1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs event)
    {
        // Possibly filter here for certain keystrokes?
        // Using e.KeyCode, e.KeyData or whatever.
        button1.PerformClick();
    }