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

如何将中的字符转换为等效的System.Windows.Input.Key枚举值?

  •  15
  • Vin  · 技术社区  · 15 年前

            public System.Windows.Input.Key ResolveKey(char charToResolve)
            {
                // Code goes here, that resolves the charToResolve
                // in to the Key enumerated value
                // (For example with '.' as the character for Key.OemPeriod)
    
            }
    

    我知道我可以写一个巨大的开关盒来匹配角色,但是还有其他方法吗? 问题是键enum的字符串可能与字符不匹配,因此enum.IsDefined将无法工作

    更新:这是在Windows环境中

    4 回复  |  直到 15 年前
        1
  •  25
  •   xcud    15 年前
    [DllImport("user32.dll")]
    static extern short VkKeyScan(char ch);
    
    static public Key ResolveKey(char charToResolve)
    {
        return KeyInterop.KeyFromVirtualKey(VkKeyScan(charToResolve));
    }
        2
  •  7
  •   Joacim Andersson    15 年前

        3
  •  1
  •   Community PPrice    7 年前

    answer for similar question from Jon Hanna 也可以处理控制键状态:

    用一个示例程序解释这一点可能比用一个示例程序解释更容易 别的:

    namespace KeyFinder
    {
      class Program
      {
        [DllImport("user32.dll", CharSet = CharSet.Unicode)]
        static extern short VkKeyScanEx(char ch, IntPtr dwhkl);
        [DllImport("user32.dll")]
        static extern bool UnloadKeyboardLayout(IntPtr hkl);
        [DllImport("user32.dll")]
        static extern IntPtr LoadKeyboardLayout(string pwszKLID, uint Flags);
        public class KeyboardPointer : IDisposable
        {
          private readonly IntPtr pointer;
          public KeyboardPointer(int klid)
          {
            pointer = LoadKeyboardLayout(klid.ToString("X8"), 1);
          }
          public KeyboardPointer(CultureInfo culture)
            :this(culture.KeyboardLayoutId){}
          public void Dispose()
          {
            UnloadKeyboardLayout(pointer);
            GC.SuppressFinalize(this);
          }
          ~KeyboardPointer()
          {
            UnloadKeyboardLayout(pointer);
          }
          // Converting to System.Windows.Forms.Key here, but
          // some other enumerations for similar tasks have the same
          // one-to-one mapping to the underlying Windows API values
          public bool GetKey(char character, out Keys key)
          {
            short keyNumber = VkKeyScanEx(character, pointer);
            if(keyNumber == -1)
            {
              key = System.Windows.Forms.Keys.None;
              return false;
            }
            key = (System.Windows.Forms.Keys)(((keyNumber & 0xFF00) << 8) | (keyNumber & 0xFF));
            return true;
          }
        }
        private static string DescribeKey(Keys key)
        {
          StringBuilder desc = new StringBuilder();
          if((key & Keys.Shift) != Keys.None)
            desc.Append("Shift: ");
          if((key & Keys.Control) != Keys.None)
            desc.Append("Control: ");
          if((key & Keys.Alt) != Keys.None)
            desc.Append("Alt: ");
          return desc.Append(key & Keys.KeyCode).ToString();
        }
        public static void Main(string[] args)
        {
          string testChars = "Aéש";
          Keys key;
          foreach(var culture in (new string[]{"he-IL", "en-US", "en-IE"}).Select(code => CultureInfo.GetCultureInfo(code)))
          {
            Console.WriteLine(culture.Name);
            using(var keyboard = new KeyboardPointer(culture))
              foreach(char test in testChars)
              {
                Console.Write(test);
                Console.Write('\t');
                if(keyboard.GetKey(test, out key))
                  Console.WriteLine(DescribeKey(key));
                else
                  Console.WriteLine("No Key");
              }
          }
          Console.Read();//Stop window closing
        }
      }
    }
    

    he-IL
    A  Shift: A
    é  No Key
    ש  A
    en-US
    A  Shift: A
    é  No Key
    ש  No Key
    en-IE
    A  Shift: A
    é  Control: Alt: E
    ש  No Key
    

    (尽管您自己的控制台可能会出现故障×和/或取决于 设置和字体)。

    从中阅读完整的描述 referenced answer

        4
  •  1
  •   Pang firemonkey    4 年前

    Dim KeyConverter As New Forms.KeysConverter    
    Dim S As String = KeyConverter.ConvertToString(e.Key)
    Dim O As System.Windows.Forms.Keys = KeyConverter.ConvertFrom(S)
    Dim ChValue As Integer = CType(O, Integer) 
    

    在我的情况下,我按键盘上的“回车键”, O 正在进入 ENTER {13} ChValue将进入字符代码 13 对于 TAB 键,我将收到字符代码 9