【问题标题】:Activate a button Process via custom KeyPress通过自定义 KeyPress 激活按钮进程
【发布时间】:2013-01-29 09:37:03
【问题描述】:

基本上在我正在处理的 C# Windows 窗体应用程序中,我按下了两个按钮 button1 和 button2。

当我同时按下2 custom keys(eg. CTRL+L)时,我如何做到这一点,程序执行编码的步骤button1?请记住,该窗口可能未处于活动状态。

我看过这个:Keypress To Simulate A Button Click in C# 但我认为如果窗口不活动,这将不起作用,而且它也只按下一个按钮而不是两个。

【问题讨论】:

标签: c# button


【解决方案1】:

在Form KeyUp Event 并且属性是KeyPressPreview = true 你可以完成这个任务。

   private void Form_KeyUp(object sender, KeyEventArgs e)
   {
       if (e.KeyCode == Keys.L && e.Control)
       {
                yourButton.PerformClick();
                e.Handled = true;
        }
    }

或

如果你想Keypress Event

private void Form_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.Control && e.KeyCode == Keys.L)
    {
            yourButton.PerformClick();
            e.Handled = true;
    }
}

编辑:

我意识到 OP 想要从他的声明中得到 keypress Event“但如果窗口不活动,我认为这不会起作用”

你可能必须检查这个

How to set a Windows hook in Visual C# .NET

【讨论】:

  • 他们这样做的更短的方法是:support.microsoft.com/kb/318804 ?
  • 正如您从链接示例中注意到的那样,很容易理解。祝你好运:)
【解决方案2】:

如果您在按钮文本中的任何字符前面加上 &,并保持 UseMnemonic 为 true,。 alt+字符将用作快捷键。

例如,在您的情况下,将按钮的文本分别更改为 button&1 和 button&2。保持 UserMnemonic = true。现在,如果你按下 alt+1,那么 button1 将被按下,如果你按下 alt+2,button2 将被按下。

1 和 2 也将分别加下划线。

希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 2019-04-06
    • 2012-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多