从char中获取KeyCode? [英] Get KeyCode from a char?

查看:210
本文介绍了从char中获取KeyCode?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够读取一个字符,并获得与其语言和键盘布局相关的键。

I need to be able to read a char and get it's Key relevant to it's language and keyboard layout.

我知道如何查看文化设置和语言。
但是我怎么能拿英文字母S,知道键盘上有什么键?
或者对于一个更难的问题,我怎么能接受字母'ש'和知道它是什么键在键盘上?

I know how to see the cultural settings and languages. But how can i take a letter like 'S' in english and know what key it is on the keyboard? Or for a harder problem, how can i take the letter 'ש' and know what key it is on the keyboard?

推荐答案

使用示例程序可以更容易地解释这一点:

This one might be more easily explained with an example program than anything else:

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

(虽然你自己的控制台可能会乱糟糟的和/或é取决于设置和字体)。

(Though your own console might mess up ש and/or é depending on settings and fonts).

注意Windows kludge使用Ctrl + Alt作为替代,如果键盘没有AltGr键,它正是如何报告,它只是在一个较低的水平,这两个被视为独立的,这是使Windows键盘不太灵活的事情之一Alt + AltGr在Windows中无意义。

Note that the Windows kludge of using Ctrl+Alt as a substitute in case a keyboard has no AltGr key is precisely how it is reported, it's only at a lower level again that the two are treated as separate, which is one of the things that makes Windows keyboards less flexible (Alt + AltGr is meaningless in Windows).

编辑: KeyboardPointer 的构造函数需要 CultureInfo 有明显的易用性,但是它需要一个数字对于给定文化的辅助键盘是有用的。例如。 en-US最经常使用0x0149,但是对于变体布局(如Dvorak),有不同的更高字(0x00010149,0x00020149,0x00030149等)的变体,扩展支持字符(所谓的国际美国需要写英语naïve,façade或résumé等字样)。

The constructor for KeyboardPointer that takes a CultureInfo has obvious ease of use, but that which takes a number is useful for secondary keyboards for a given culture. E.g. en-US most often uses 0x0149, but there are variants with a different higher word (0x00010149, 0x00020149, 0x00030149, etc.) for variant layouts like Dvorak, extended support for characters (the so-called "International US" needed to write English words like "naïve", "façade" or "résumé"), and so on.

这篇关于从char中获取KeyCode?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆