用户按键轻松设置热键。显示热键文本,保存虚拟键码

时间:2013-07-05 22:18:58

标签: c# winforms

我不确定我是否对标题说得足够好,但是我希望有人按任意键然后我可以存储该密钥代码以供日后使用,但更重要的是我希望能够直观地显示他们选择的密钥。有点像游戏中的键盘设置类型。

我想知道是否有任何公共课程或某些东西可以减轻为每个密钥编码的麻烦。

2 个答案:

答案 0 :(得分:2)

为此目的明确设计了一个本机Win32控件,称为Hot Key控件。您可能已经在Windows shell和其他应用程序中看到它正在使用中。那将是我建议你使用的那个,而不是试图自己重新发明轮子。

不幸的是,WinForms没有为此控件提供包装器。这意味着你要么自己写,要么选择someone else's implementation of a similar type of control

编辑:或者使用这个快速结合的.NET包装器来进行热键控制。整个公共接口是KeyData属性,其作用类似于KeyEventArgs类的类似命名属性。 Text属性(由继承自Control提供)不会执行任何操作,但可能会重载以返回所选热键的精美版本。此外,本机控件的HKM_SETRULES消息的功能未实现;如果您需要该功能,则需要自己添加代码。

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

internal static class NativeMethods
{
   internal const string HOTKEY_CLASS = "msctls_hotkey32";
   internal const int CS_GLOBALCLASS = 0x4000;

   internal const int WS_CHILD = 0x40000000;
   internal const int WS_VISIBLE = 0x10000000;
   internal const int WS_TABSTOP = 0x00010000;
   internal const int WS_EX_NOPARENTNOTIFY = 0x00000004;
   internal const int WS_EX_CLIENTEDGE = 0x00000200;
   internal const int WS_EX_LEFT = 0x00000000;
   internal const int WS_EX_LTRREADING = 0x00000000;
   internal const int WS_EX_RIGHTSCROLLBAR = 0x00000000;
   internal const int WS_EX_RIGHT = 0x00001000;
   internal const int WS_EX_RTLREADING = 0x00002000;
   internal const int WS_EX_LEFTSCROLLBAR = 0x00004000;

   internal const int HOTKEYF_SHIFT = 0x01;
   internal const int HOTKEYF_CONTROL = 0x02;
   internal const int HOTKEYF_ALT = 0x04;
   internal const int HOTKEYF_EXT = 0x08;

   internal const int WM_USER = 0x0400;
   internal const int HKM_SETHOTKEY = (WM_USER + 1);
   internal const int HKM_GETHOTKEY = (WM_USER + 2);
   internal const int HKM_SETRULES = (WM_USER + 3);

   [DllImport("user32.dll", CharSet = CharSet.Auto)]
   internal static extern IntPtr SendMessage(IntPtr hWnd,
                                             int msg,
                                             IntPtr wParam,
                                             IntPtr lParam);
}

class HotKey : Control
{
   public HotKey()
   {
      base.SetStyle(ControlStyles.UserPaint
                     | ControlStyles.StandardClick
                     | ControlStyles.StandardDoubleClick
                     | ControlStyles.UseTextForAccessibility, false);
      base.SetStyle(ControlStyles.FixedHeight, true);
   }

   public Keys KeyData
   {
      get
      {
         IntPtr retVal = NativeMethods.SendMessage(Handle,
                                                   NativeMethods.HKM_GETHOTKEY,
                                                   IntPtr.Zero,
                                                   IntPtr.Zero);

         Keys keyCode = (Keys)(retVal.ToInt32() & 0xFF);

         int modifierFlags = (retVal.ToInt32() >> 8);
         Keys modifiers = Keys.None;
         if ((modifierFlags & NativeMethods.HOTKEYF_ALT) == NativeMethods.HOTKEYF_ALT)
            modifiers |= Keys.Alt;
         if ((modifierFlags & NativeMethods.HOTKEYF_CONTROL) == NativeMethods.HOTKEYF_CONTROL)
            modifiers |= Keys.Control;
         if ((modifierFlags & NativeMethods.HOTKEYF_SHIFT) == NativeMethods.HOTKEYF_SHIFT)
            modifiers |= Keys.Shift;

         return (keyCode | modifiers);
      }
      set
      {
         Keys keyCode = (value & (~Keys.Alt & ~Keys.Control & ~Keys.Shift));

         int modifierFlags = 0;
         if ((value & Keys.Alt) == Keys.Alt)
            modifierFlags |= NativeMethods.HOTKEYF_ALT;
         if ((value & Keys.Control) == Keys.Control)
            modifierFlags |= NativeMethods.HOTKEYF_CONTROL;
         if ((value & Keys.Shift) == Keys.Shift)
            modifierFlags |= NativeMethods.HOTKEYF_SHIFT;

         NativeMethods.SendMessage(Handle,
                                   NativeMethods.HKM_SETHOTKEY,
                                   (IntPtr)((modifierFlags << 8) | ((int)keyCode & 0xffff)),
                                   IntPtr.Zero);
      }
   }

   protected override CreateParams CreateParams
   {
      get
      {
         CreateParams cp = base.CreateParams;
         cp.ClassName = NativeMethods.HOTKEY_CLASS;
         cp.ClassStyle = NativeMethods.CS_GLOBALCLASS;
         cp.Style = NativeMethods.WS_CHILD | NativeMethods.WS_VISIBLE | NativeMethods.WS_TABSTOP;
         cp.ExStyle = NativeMethods.WS_EX_NOPARENTNOTIFY | NativeMethods.WS_EX_CLIENTEDGE;
         if (RightToLeft == RightToLeft.No ||
            (RightToLeft == RightToLeft.Inherit && Parent.RightToLeft == RightToLeft.No))
         {
            cp.ExStyle |= NativeMethods.WS_EX_LEFT
                            | NativeMethods.WS_EX_LTRREADING
                            | NativeMethods.WS_EX_RIGHTSCROLLBAR;
         }
         else
         {
            cp.ExStyle |= NativeMethods.WS_EX_RIGHT
                           | NativeMethods.WS_EX_RTLREADING
                           | NativeMethods.WS_EX_LEFTSCROLLBAR;
         }
         return cp;
      }
   }
}

答案 1 :(得分:0)

看看Key press events in C# -- Moving a PictureBox

它提到了Control.KeyDown事件。

private Keys _lastKeyPressed;

public Form1()
{
    InitializeComponent();
    this.KeyDown += Form1_KeyDown;
}

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    // Do something with event keycode
    _lastKeyPressed = e.KeyCode;
}

您还可以在发布密钥时收听Control.KeyUp事件。