Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6670533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:16:49+00:00 2026-05-26T03:16:49+00:00

I am using the Raw Input API to get a collection of key presses

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T03:16:50+00:00Added an answer on May 26, 2026 at 3:16 am

    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];
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to read a raw input stream from php using php://input .
Is it possible to use Windows Raw Input API without a window (ie from
Note: Using raw Win32 CreateTheard() API No MFC An interface is simply a pointer
Is there a way/tool to auto convert Java source code from using raw types
I'm trying to insert rows using raw sql in django: The input file has
Other than using raw XML, is there an easy way in .NET to open
i am using raw sockets to create my own socket. i need to set
Writing an asynchronous Ping using Raw Sockets in F#, to enable parallel requests using
I'm compressing raw AVI files to WMV using the ASF Writer. I'm in a
How can I do a RAW POST in PHP using cURL? Raw post as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.