从使用Raw Input API检索的按键集合中获取字符串 [英] Get a String from a collection of keys presses retrieved using Raw Input API
问题描述
我正在使用Raw Input API从键盘(实际上是模拟键盘的磁条读卡器)上收集按键的集合.这是一些代码摘录,因此您可以了解我如何获取密钥.
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);
运行代码时,读卡器输出字符串%B40^TEST
,但是在MyKeys集合中,我具有以下值:
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' }
这些看起来像是实际按键的集合( duh!),而不是它们所代表的字符串.按键代码16似乎是 Shift ,因此在读卡器当前配置的键盘映射中,使用 Shift + 5 生成了%
字符,由{16,53}表示.大写字母B
以下字符是 Shift + B 或{16,66}.其他字符也是如此.
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.
很明显,简单地将它们强制转换为char
(就像我现在正在做的那样)并不是可行的方法.所以,我的问题是:如何将这一系列按键转换成它们代表的字符串?
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?
推荐答案
进行了一些额外的研究后,我自己找到了答案.我会将其发布给其他阅读此书的人.以下是一个小型测试应用程序,该应用程序演示了如何将虚拟键(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.
class Program
{
[DllImport("user32.dll")]
static extern int MapVirtualKey(uint uCode, uint uMapType);
[DllImport("user32.dll")]
private static extern int ToAscii(uint uVirtKey, uint uScanCode, byte[] lpKeyState, [Out] StringBuilder lpChar, uint uFlags);
static void Main(string[] args)
{
byte[] keyState = new byte[256];
ushort[] input = { 16, 53, 16, 66, 52, 48, 16, 54, 16, 84, 16, 69, 16, 83, 16, 84 };
StringBuilder output = new StringBuilder();
foreach (ushort vk in input)
AppendChar(output, vk, ref keyState);
Console.WriteLine(output);
Console.ReadKey(true);
}
private static void AppendChar(StringBuilder output, ushort vKey, ref byte[] keyState)
{
if (MapVirtualKey(vKey, 2) == 0)
{
keyState[vKey] = 0x80;
}
else
{
StringBuilder chr = new StringBuilder(2);
int n = ToAscii(vKey, 0, keyState, chr, 0);
if (n > 0)
output.Append(chr.ToString(0, n));
keyState = new byte[256];
}
}
}
这篇关于从使用Raw Input API检索的按键集合中获取字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!