代码之家  ›  专栏  ›  技术社区  ›  Rockcoder

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

  •  261
  • Rockcoder  · 技术社区  · 17 年前

    我正在寻找实现常用Windows键盘快捷键的最佳方法(例如 Ctrl键 + F , Ctrl键 N Windows Forms

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

    我在考虑用这样的东西在 子窗体\u键关闭 活动:

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

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

    11 回复  |  直到 13 年前
        1
  •  477
  •   Bridge user2173966    12 年前

    您可能忘了设置表单的 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);
    }
    
        2
  •  80
  •   bluish dmajkic    12 年前

    在你的主窗体上

    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();
          }
      }
      
        3
  •  20
  •   Konrad Rudolph    17 年前

    最好的方法是使用菜单助记符,即在主窗体中设置菜单项,并为其指定所需的键盘快捷键。然后所有其他事情都在内部处理,您所要做的就是实现在 Click 该菜单项的事件处理程序。

        4
  •  11
  •   slavoo user3099232    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...
    }
    
        5
  •  8
  •   Peter Mortensen Pieter Jan Bonestroo    13 年前

    如果你有菜单,那么改变 ShortcutKeys ToolStripMenuItem 我们应该做到这一点。

    如果没有,您可以创建一个并设置其 visible

        6
  •  6
  •   slavoo user3099232    10 年前

    在主窗体中,您必须:

    • 一定要做好准备 按键预览 到 符合事实的 (默认情况下为TRUE)
    • MainForm_键关闭 (..)-您可以在此处设置任何想要的快捷方式。

    我认为您必须使用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/

        7
  •  4
  •   Community Mohan Dere    9 年前

    Hans's answer 对新手来说可能会更容易一些,所以这是我的版本。

    你不必愚弄我 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();
        }
    }
    
        8
  •  2
  •   s k    9 年前

    在WinForm中,我们始终可以通过以下方式获取控制键状态:

    bool IsCtrlPressed = (Control.ModifierKeys & Keys.Control) != 0;