Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a String from a collection of keys presses retrieved using 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);

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?

like image 563
MarioVW Avatar asked Oct 07 '11 15:10

MarioVW


1 Answers

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];
        }
    }
}
like image 150
MarioVW Avatar answered Nov 09 '22 20:11

MarioVW