• <bdo id='GIZrY'></bdo><ul id='GIZrY'></ul>
        <tfoot id='GIZrY'></tfoot>

      1. <small id='GIZrY'></small><noframes id='GIZrY'>

      2. <i id='GIZrY'><tr id='GIZrY'><dt id='GIZrY'><q id='GIZrY'><span id='GIZrY'><b id='GIZrY'><form id='GIZrY'><ins id='GIZrY'></ins><ul id='GIZrY'></ul><sub id='GIZrY'></sub></form><legend id='GIZrY'></legend><bdo id='GIZrY'><pre id='GIZrY'><center id='GIZrY'></center></pre></bdo></b><th id='GIZrY'></th></span></q></dt></tr></i><div id='GIZrY'><tfoot id='GIZrY'></tfoot><dl id='GIZrY'><fieldset id='GIZrY'></fieldset></dl></div>
        <legend id='GIZrY'><style id='GIZrY'><dir id='GIZrY'><q id='GIZrY'></q></dir></style></legend>

        .NET 中的键盘映射

        时间:2023-07-26
            1. <small id='3DjUn'></small><noframes id='3DjUn'>

                  <bdo id='3DjUn'></bdo><ul id='3DjUn'></ul>
                    <tbody id='3DjUn'></tbody>
                  <i id='3DjUn'><tr id='3DjUn'><dt id='3DjUn'><q id='3DjUn'><span id='3DjUn'><b id='3DjUn'><form id='3DjUn'><ins id='3DjUn'></ins><ul id='3DjUn'></ul><sub id='3DjUn'></sub></form><legend id='3DjUn'></legend><bdo id='3DjUn'><pre id='3DjUn'><center id='3DjUn'></center></pre></bdo></b><th id='3DjUn'></th></span></q></dt></tr></i><div id='3DjUn'><tfoot id='3DjUn'></tfoot><dl id='3DjUn'><fieldset id='3DjUn'></fieldset></dl></div>
                  <legend id='3DjUn'><style id='3DjUn'><dir id='3DjUn'><q id='3DjUn'></q></dir></style></legend>

                  <tfoot id='3DjUn'></tfoot>
                • 本文介绍了.NET 中的键盘映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  如果我知道某个键已被按下(例如 Key.D3),并且 Shift 键也按下(Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift)),我怎样才能找出指的是什么字符(例如,美国键盘上的 #,英国的英镑符号键盘等)?

                  If I know that a certain key has been pressed (eg Key.D3), and that the Shift key is also down (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift)), how can I find out what character that refers to (eg, # on US keyboard, UK pound sign on UK keyboard, etc)?

                  换句话说,我如何以编程方式找出 Shift + 3 产生 # (它不会在非-美式键盘).

                  Put another way, how can I find out, programatically, that Shift + 3 produces # (it wouldn't on a non-US keyboard).

                  推荐答案

                  如果你想确定你将从给定的键和给定的修饰符中得到什么字符,你应该使用 user32 ToAscii 函数.或者 ToAsciiEx键盘布局 other 然后是当前的.

                  If you want to determine what character you will get from a given key with given modifiers, you should use the user32 ToAscii function. Or ToAsciiEx if you want to use a keyboard layout other then the current one.

                  using System.Runtime.InteropServices;
                  public static class User32Interop
                  {
                    public static char ToAscii(Keys key, Keys modifiers)
                    {
                      var outputBuilder = new StringBuilder(2);
                      int result = ToAscii((uint)key, 0, GetKeyState(modifiers),
                                           outputBuilder, 0);
                      if (result == 1)
                        return outputBuilder[0];
                      else
                        throw new Exception("Invalid key");
                    }
                  
                    private const byte HighBit = 0x80;
                    private static byte[] GetKeyState(Keys modifiers)
                    {
                      var keyState = new byte[256];
                      foreach (Keys key in Enum.GetValues(typeof(Keys)))
                      {
                        if ((modifiers & key) == key)
                        {
                          keyState[(int)key] = HighBit;
                        }
                      }
                      return keyState;
                    }
                  
                    [DllImport("user32.dll")]
                    private static extern int ToAscii(uint uVirtKey, uint uScanCode,
                                                      byte[] lpKeyState,
                                                      [Out] StringBuilder lpChar,
                                                      uint uFlags);
                  }
                  

                  您现在可以像这样使用它:

                  You can now use it like this:

                  char c = User32Interop.ToAscii(Keys.D3, Keys.ShiftKey); // = '#'
                  

                  如果您需要多个修饰符,只需它们即可.Keys.ShiftKey |Keys.AltKey

                  If you need more than one modifier, just or them. Keys.ShiftKey | Keys.AltKey

                  这篇关于.NET 中的键盘映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何将密钥而不是字符发送到进程? 下一篇:如何检测 WPF 中的修饰键状态?

                  相关文章

                  <i id='SWdWg'><tr id='SWdWg'><dt id='SWdWg'><q id='SWdWg'><span id='SWdWg'><b id='SWdWg'><form id='SWdWg'><ins id='SWdWg'></ins><ul id='SWdWg'></ul><sub id='SWdWg'></sub></form><legend id='SWdWg'></legend><bdo id='SWdWg'><pre id='SWdWg'><center id='SWdWg'></center></pre></bdo></b><th id='SWdWg'></th></span></q></dt></tr></i><div id='SWdWg'><tfoot id='SWdWg'></tfoot><dl id='SWdWg'><fieldset id='SWdWg'></fieldset></dl></div>

                      <bdo id='SWdWg'></bdo><ul id='SWdWg'></ul>

                    <small id='SWdWg'></small><noframes id='SWdWg'>

                      <legend id='SWdWg'><style id='SWdWg'><dir id='SWdWg'><q id='SWdWg'></q></dir></style></legend>
                      <tfoot id='SWdWg'></tfoot>