将 PreviewKeyDown 中收到的密钥转换为字符串
我在窗口上使用 PreviewKeyDown 事件来接收来自条形码扫描仪的所有键。 KeyEventArgs 是一个枚举,没有给我实际的字符串。我不想使用 TextInput,因为某些键可能由控件本身处理,并且可能不会冒泡到 TextInput 事件。
我正在寻找一种方法将 PreviewKeyDown 中获得的键转换为实际字符串。 我查看了 InputManager、TextCompositionManager 等,但没有找到一种方法来给出键列表并返回一个字符串。 TextCompositionManager 或其他东西必须将这些键转换为 TextInput 中可用的字符串。
I am using PreviewKeyDown event on a window to receive all the keys from a barcode scanner. The KeyEventArgs is an enumeration and does not given me the actual string. I dont want to use TextInput as some of the keys may get handled by the control itself and may not bubble up to the TextInput event.
I am looking for a way to convert the the Keys that I get in PreviewKeyDown to actual string.
I looked at the InputManager, TextCompositionManager etc but I am not finding a way where I give the list of keys and it comes back with a string.
TextCompositionManager or something must be converting these Keys to a string which is what is available in TextInput.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我正在使用的事件。 KeyDown 获取按键,PreviewTextInput 获取实际文本。因此,在按键之间的某个位置正在转换为文本。
Here is the event that I am using. The KeyDown gets the keys and the PreviewTextInput gets the actual text. So somewhere in between the keys are getting converted to text.
按键->文本转换比您想象的要复杂得多,实际上没有办法将单个击键映射到单个字符,因为在某些语言和某些情况下,您需要多次击键才能组成单个字符。
由于您对条形码扫描仪的输入感兴趣(我假设只会生成 Windows 可以处理的一小部分,可能只有 ASCII,甚至更少),您可以自己构建转换表并将其硬编码到您的程序中 - 这非常多然后更容易处理 Windows 文本处理所做的所有疯狂事情(为了好玩,查找“死键”)。
Key -> Text conversion is much more complicated than you think, there is actually no way to map a single key stroke to a single character because in some languages and some cases you need multiple keystrokes to compose a single character.
Since you are interested in input from a barcode scanner (that I assume will only generate a small subset of what windows can handle, maybe only ASCII maybe even less) you can build the conversion table yourself and hard code it into your program - it's much easier then to handle all the craziness that Windows text handling does (for fun, lookup "dead keys").