栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在WPF / C#中的不同区域设置键盘上捕获“#”字符?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在WPF / C#中的不同区域设置键盘上捕获“#”字符?

下面的函数GetCharFromKey(Key key)可以解决问题。

它使用一系列win32调用来解码按下的键:

  1. 从WPF密钥获取虚拟密钥

  2. 从虚拟密钥获取扫描代码

  3. 得到你的Unipre字符

这篇旧文章对此进行了更详细的描述。

      public enum MapType : uint      {         MAPVK_VK_TO_VSC = 0x0,         MAPVK_VSC_TO_VK = 0x1,         MAPVK_VK_TO_CHAR = 0x2,         MAPVK_VSC_TO_VK_EX = 0x3,      }      [Dllimport("user32.dll")]      public static extern int ToUnipre(          uint wVirtKey,          uint wScanCode,          byte[] lpKeyState,          [Out, MarshalAs(UnmanagedType.LPWStr, SizeParamIndex = 4)]  StringBuilder pwszBuff,          int cchBuff,          uint wFlags);      [Dllimport("user32.dll")]      public static extern bool GetKeyboardState(byte[] lpKeyState);      [Dllimport("user32.dll")]      public static extern uint MapVirtualKey(uint uCode, MapType uMapType);      public static char GetCharFromKey(Key key)      {         char ch = ' ';         int virtualKey = KeyInterop.VirtualKeyFromKey(key);         byte[] keyboardState = new byte[256];         GetKeyboardState(keyboardState);         uint scanCode = MapVirtualKey((uint)virtualKey, MapType.MAPVK_VK_TO_VSC);         StringBuilder stringBuilder = new StringBuilder(2);         int result = ToUnipre((uint)virtualKey, scanCode, keyboardState, stringBuilder, stringBuilder.Capacity, 0);         switch (result)         { case -1:     break; case 0:     break; case 1:    {       ch = stringBuilder[0];       break;    } default:    {       ch = stringBuilder[0];       break;    }         }         return ch;      }


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/432923.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号