从使用原始输入 API 检索的按键集合中获取字符串
我正在使用原始输入 API 从键盘(实际上是模拟键盘的磁条卡读卡器)获取按键的集合。这里有一些代码摘录,以便您可以了解我如何获取密钥。
[StructLayout(LayoutKind.Sequential)]
internal struct RAWKEYBOARD
{
[MarshalAs(UnmanagedType.U2)]
public ushort MakeCode;
[MarshalAs(UnmanagedType.U2)]
public ushort Flags;
[MarshalAs(UnmanagedType.U2)]
public ushort Reserved;
[MarshalAs(UnmanagedType.U2)]
public ushort VKey;
[MarshalAs(UnmanagedType.U4)]
public uint Message;
[MarshalAs(UnmanagedType.U4)]
public uint ExtraInformation;
}
[StructLayout(LayoutKind.Explicit)]
internal struct RAWINPUT
{
[FieldOffset(0)]
public RAWINPUTHEADER header;
[FieldOffset(16)]
public RAWMOUSE mouse;
[FieldOffset(16)]
public RAWKEYBOARD keyboard;
[FieldOffset(16)]
public RAWHID hid;
}
Queue<char> MyKeys = new Queue<char>();
// buffer has the result of a GetRawInputData() call
RAWINPUT raw = (RAWINPUT)Marshal.PtrToStructure(buffer, typeof(RAWINPUT));
MyKeys.Enqueue((char)raw.keyboard.VKey);
运行代码时,读卡器输出字符串 %B40^TEST
,但在 MyKeys 集合中,我有以下值:
{ 16 '', 53 '5', 16 '', 66 'B',
52 '4', 48 '0', 16 '', 54 '6',
16 '', 84 'T', 16 '', 69 'E',
16 '', 83 'S', 16 '', 84 'T' }
这些看起来像是实际按键的集合(duh!< /em>) 而不是它们代表的字符串。键码 16 似乎是 Shift,因此在读卡器当前配置的键盘映射中,使用 Shift+5 生成 %
字符,表示为 { 16, 53}。接下来的大写字符 B
是 Shift+B 或 {16, 66}。其余角色也是如此。
显然,简单地将它们转换为 char
(就像我现在所做的那样)并不是正确的方法。所以,我的问题是:如何将这个按键数组转换为它们代表的字符串?
I am using the Raw Input API to get a collection of key presses from a keyboard (actually, a magnetic stripe card reader that emulates a keyboard). Here are a couple of code excerpts so you can have an idea of how I'm getting the keys.
[StructLayout(LayoutKind.Sequential)]
internal struct RAWKEYBOARD
{
[MarshalAs(UnmanagedType.U2)]
public ushort MakeCode;
[MarshalAs(UnmanagedType.U2)]
public ushort Flags;
[MarshalAs(UnmanagedType.U2)]
public ushort Reserved;
[MarshalAs(UnmanagedType.U2)]
public ushort VKey;
[MarshalAs(UnmanagedType.U4)]
public uint Message;
[MarshalAs(UnmanagedType.U4)]
public uint ExtraInformation;
}
[StructLayout(LayoutKind.Explicit)]
internal struct RAWINPUT
{
[FieldOffset(0)]
public RAWINPUTHEADER header;
[FieldOffset(16)]
public RAWMOUSE mouse;
[FieldOffset(16)]
public RAWKEYBOARD keyboard;
[FieldOffset(16)]
public RAWHID hid;
}
Queue<char> MyKeys = new Queue<char>();
// buffer has the result of a GetRawInputData() call
RAWINPUT raw = (RAWINPUT)Marshal.PtrToStructure(buffer, typeof(RAWINPUT));
MyKeys.Enqueue((char)raw.keyboard.VKey);
When running the code, the card reader outputs the string %B40^TEST
, but in the MyKeys collection I have the following values:
{ 16 '', 53 '5', 16 '', 66 'B',
52 '4', 48 '0', 16 '', 54 '6',
16 '', 84 'T', 16 '', 69 'E',
16 '', 83 'S', 16 '', 84 'T' }
These seem like a collection of actual key presses (duh!) and not the string they represent. Keycode 16 seems to be Shift, so in the card reader's currently configured keyboard mapping a %
character is produced using Shift+5, represented by {16, 53}. The following character, uppercase B
, is Shift+B or {16, 66}. And so it goes for the rest of characters.
Obviously, simply casting these to char
(like I'm doing right now) is not the way to go. So, my question is: How can I translate this array of key presses into the String they represent?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过一些额外的研究后,我自己找到了答案。我将其发布给其他阅读此内容的人。以下是一个小型测试应用程序,演示如何将虚拟键(
ushort
键代码)集合转换为其字符串表示形式。我使用问题中描述的集合作为输入。After doing some extra research, I found the answer myself. I'm posting it for anyone else reading this. Following is a small test application that demonstrates how to convert a collection of virtual keys (
ushort
key codes) into its string representation. I'm using the collection described in the question as the input.