按钮的键盘快捷键

发布于 2024-12-11 19:41:37 字数 432 浏览 3 评论 0原文

在 C# (Microsoft Visual Studio 2010) 中,如何为像下面这样的按钮?

    private void closeButton_Click(object sender, EventArgs e)
    {
        // Close the program
        this.Close();
    }

我知道我可以使用“&”按钮文本中的字符并创建一个 Alt - n 快捷方式,但我想创建一个按键快捷方式,例如 c 来执行上面的操作。

In C# (Microsoft Visual Studio 2010), how can I assign a keyboard shortcut to a button such as the following?

    private void closeButton_Click(object sender, EventArgs e)
    {
        // Close the program
        this.Close();
    }

I know I can use the "&" character in the button's Text and create an Alt - n shortcut, but I'd like to create a single keypress shortcut, such as c to execute the above.

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

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

发布评论

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

评论(3

稳稳的幸福 2024-12-18 19:41:37

KeyDown 是你的朋友;)例如,如果你想要在按下 Shift 时使用快捷键 A,请尝试以下操作:

    void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.A && e.Shift) 
            // Do something
    }

如果你想要一个“真正的”键盘快捷键,你可以使用钩子。
查看 Stack Overflow 问题RegisterHotKeys 和全局键盘挂钩?

KeyDown is your friend ;) For example, if you want the shortcut key A while Shift is pressed, try this:

    void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.A && e.Shift) 
            // Do something
    }

If you want a "real" keyboard shortcut you can use hooks.
Look at Stack Overflow question RegisterHotKeys and global keyboard hooks?.

我是有多爱你 2024-12-18 19:41:37

这是一种不同的方法:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    switch(keyData)
    {
         case Keys.F2:
             // do something...
             return true;
         case Keys.F3:
             // do something...
             return true;
         case Keys.F4:
             // do something...
             return true;
         default:
             return base.ProcessCmdKey(ref msg, keyData);
    }            
}

您不需要更改 KeyPreview 值。

Here's a different approach:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    switch(keyData)
    {
         case Keys.F2:
             // do something...
             return true;
         case Keys.F3:
             // do something...
             return true;
         case Keys.F4:
             // do something...
             return true;
         default:
             return base.ProcessCmdKey(ref msg, keyData);
    }            
}

You don't need to change KeyPreview value.

黎歌 2024-12-18 19:41:37

如果你想在 switch 语句中添加 Ctrl + S ,那么你也可以尝试下面的代码。

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        switch (keyData)
        {
            case Keys.Control | Keys.S:
                // do something...
                return true;
            case Keys.Control |Keys.Alt | Keys.S:
                // do something...
                return true;
            case Keys.F2:
                // do something...
                return true;
            case Keys.F3:
                // do something...
                return true;
            case Keys.F4:
                // do something...
                return true;
            default:
                return base.ProcessCmdKey(ref msg, keyData);
        }
    }

if you want add Ctrl + S in switch statment so you can also try below code.

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        switch (keyData)
        {
            case Keys.Control | Keys.S:
                // do something...
                return true;
            case Keys.Control |Keys.Alt | Keys.S:
                // do something...
                return true;
            case Keys.F2:
                // do something...
                return true;
            case Keys.F3:
                // do something...
                return true;
            case Keys.F4:
                // do something...
                return true;
            default:
                return base.ProcessCmdKey(ref msg, keyData);
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文