在Windows窗体应用程序中实现键盘快捷方式的最佳方法?

Roc*_*der 273 c# keyboard-shortcuts winforms

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

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

我想在ChildForm_KeyDown事件中使用这样的东西:

   if (e.KeyCode == Keys.F && Control.ModifierKeys == Keys.Control)
        // Show search form
Run Code Online (Sandbox Code Playgroud)

但这不起作用.按键时,事件甚至不会触发.解决办法是什么?

Han*_*ant 450

您可能忘记将表单的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);
}
Run Code Online (Sandbox Code Playgroud)

  • “什么是Ctrl + F?” 大声笑 (8认同)
  • 这很有效,但只有当表单是活动窗口时才为我检测到它。有人知道如何绑定它以便在任何窗口视图中都可以吗? (2认同)

小智 70

在您的主表单上

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

    private void MainForm_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Control && e.KeyCode == Keys.N)
        {
            SearchForm searchForm = new SearchForm();
            searchForm.Show();
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)

  • + for,将KeyPreview设置为True ..失踪了 (4认同)

Kon*_*lph 20

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


小智 10

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

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...
}
Run Code Online (Sandbox Code Playgroud)


Cor*_*kie 8

如果你有一个菜单然后改变ShortcutKeys属性ToolStripMenuItem应该做的伎俩.

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


小智 5

在主表单中,您必须:

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

此外,我已经在Google上找到了此内容,并希望将其分享给仍在寻找答案的用户。(针对全球)

我认为您必须使用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
    }
}
Run Code Online (Sandbox Code Playgroud)

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