296

我正在寻找一种在 C#中的Windows 窗体应用程序中实现常用 Windows 键盘快捷键(例如Ctrl+ FCtrl+ N)的最佳方法。

该应用程序有一个主窗体,它承载许多子窗体(一次一个)。当用户点击Ctrl+F时,我想显示一个自定义搜索表单。搜索表单将取决于应用程序中当前打开的子表单。

我正在考虑在ChildForm_KeyDown事件中使用类似的东西:

   if (e.KeyCode == Keys.F && Control.ModifierKeys == Keys.Control)
        // Show search form

但这不起作用。当您按下某个键时,该事件甚至不会触发。解决办法是什么?

4

8 回答 8

480

您可能忘记将表单的KeyPreview属性设置为 True。覆盖ProcessCmdKey()方法是通用解决方案:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
  if (keyData == (Keys.Control | Keys.F)) {
    MessageBox.Show("What the Ctrl+F?");
    return true;
  }
  return base.ProcessCmdKey(ref msg, keyData);
}
于 2008-12-30T14:04:27.800 回答
80

在您的主窗体上

  1. 设置KeyPreview为真
  2. 使用以下代码添加 KeyDown 事件处理程序

    private void MainForm_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Control && e.KeyCode == Keys.N)
        {
            SearchForm searchForm = new SearchForm();
            searchForm.Show();
        }
    }
    
于 2011-08-04T07:06:48.560 回答
20

最好的方法是使用菜单助记符,即在主窗体中设置菜单条目,并为其分配所需的键盘快捷键。然后其他一切都在内部处理,您所要做的就是实现在Click该菜单条目的事件处理程序中执行的适当操作。

于 2008-12-30T12:06:27.400 回答
11

你甚至可以试试这个例子:

public class MDIParent : System.Windows.Forms.Form
{
    public bool NextTab()
    {
         // some code
    }

    public bool PreviousTab()
    {
         // some code
    }

    protected override bool ProcessCmdKey(ref Message message, Keys keys)
    {
        switch (keys)
        {
            case Keys.Control | Keys.Tab:
              {
                NextTab();
                return true;
              }
            case Keys.Control | Keys.Shift | Keys.Tab:
              {
                PreviousTab();
                return true;
              }
        }
        return base.ProcessCmdKey(ref message, keys);
    }
}

public class mySecondForm : System.Windows.Forms.Form
{
    // some code...
}
于 2012-05-11T18:07:49.643 回答
8

如果你有一个菜单,那么改变它ShortcutKeys的属性ToolStripMenuItem应该可以解决问题。

如果没有,您可以创建一个并将其visible属性设置为 false。

于 2008-12-30T14:17:16.060 回答
6

在主表单中,您必须:

  • 确保将KeyPreview设置为true(默认为 TRUE)
  • 添加MainForm_KeyDown (..) - 通过它您可以在此处设置您想要的任何快捷方式。

另外,我在谷歌上找到了这个,我想把这个分享给那些仍在寻找答案的人。(对于全球)

我认为你必须使用 user32.dll

protected override void WndProc(ref Message m)
{
    base.WndProc(ref m);

    if (m.Msg == 0x0312)
    {
        /* Note that the three lines below are not needed if you only want to register one hotkey.
         * The below lines are useful in case you want to register multiple keys, which you can use a switch with the id as argument, or if you want to know which key/modifier was pressed for some particular reason. */

        Keys key = (Keys)(((int)m.LParam >> 16) & 0xFFFF);                  // The key of the hotkey that was pressed.
        KeyModifier modifier = (KeyModifier)((int)m.LParam & 0xFFFF);       // The modifier of the hotkey that was pressed.
        int id = m.WParam.ToInt32();                                        // The id of the hotkey that was pressed.


        MessageBox.Show("Hotkey has been pressed!");
        // do something
    }
}

进一步阅读此http://www.fluxbytes.com/csharp/how-to-register-a-global-hotkey-for-your-application-in-c/

于 2015-11-16T06:19:55.400 回答
4

对于新手来说,汉斯的答案可能会更容易一些,所以这是我的版本。

您无需胡闹KeyPreview,将其设置为false. 要使用下面的代码,只需将其粘贴到您的下方form1_load并运行F5以查看它是否有效:

protected override void OnKeyPress(KeyPressEventArgs ex)
{
    string xo = ex.KeyChar.ToString();

    if (xo == "q") //You pressed "q" key on the keyboard
    {
        Form2 f2 = new Form2();
        f2.Show();
    }
}
于 2011-09-09T11:13:21.160 回答
2

在 WinForm 中,我们总是可以通过以下方式获取 Control Key 状态:

bool IsCtrlPressed = (Control.ModifierKeys & Keys.Control) != 0;
于 2016-12-31T08:30:05.057 回答