从使用原始输入 API 检索的按键集合中获取字符串

发布于 2024-12-08 23:32:08 字数 1571 浏览 0 评论 0原文

我正在使用原始输入 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}。接下来的大写字符 BShift+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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒中人 2024-12-15 23:32:08

经过一些额外的研究后,我自己找到了答案。我将其发布给其他阅读此内容的人。以下是一个小型测试应用程序,演示如何将虚拟键(ushort 键代码)集合转换为其字符串表示形式。我使用问题中描述的集合作为输入。

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];
        }
    }
}

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];
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文