Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read input from USB PC Remote

Tags:

c#

usb

I've got a Remote for my PC, which connects via USB. It requires no drivers, but some multimedia keys are not supported. And i want to know is there a method to read the input data so I can handle the events myself?

Edit: I've found a software called USBlyzer which shows me all the information I need, and is capable of handling usb events. the problem is that it's closed source

Edit: here is the same question: https://superuser.com/questions/179457/software-to-customise-this-usb-pc-remote-control but still no answer for me. I have this remote


1 Answers

I think RawInput is what You need. Call RegisterRawInputDevices (using platform invoke) to subscribe to events. Then override WndProc of main window. And call GetRawInputData to read and parse raw data.

Example:

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace StackOverflow
{
    static class Program
    {
        static void Main()
        {
            Application.Run(new Form1());
        }
    }

    public static class KeyboardRawInput
    {
        public struct RawKeyboard
        {
            public int Type;
            public int Size;
            public IntPtr Device;
            public IntPtr WParam;

            public ushort MakeCode;
            public ushort Flags;
            public ushort Reserved;
            public ushort VKey;
            public uint Message;
            public uint ExtraInformation;

        }

        public struct RawInputDevice
        {
            public ushort Page;
            public ushort Usage;
            public int Flags;
            public IntPtr HWnd;
        }


        [DllImport("user32", SetLastError = true)]
        public static extern bool RegisterRawInputDevices(
            [MarshalAs(UnmanagedType.LPArray)] RawInputDevice[] devs,
            uint count,
            int structSize);

        [DllImport("user32")]
        public static extern uint GetRawInputData(
            IntPtr hrawInput,
            uint command,
            ref RawKeyboard data,
            ref uint size,
            int headerSize);
    }

    class Form1 : Form
    {
        protected override void OnLoad(EventArgs e)
        {
            try {
                KeyboardRawInput.RawInputDevice dev = new KeyboardRawInput.RawInputDevice();
                dev.Page = 1;
                dev.Usage = 6;
                dev.Flags = 0x00000100 /*RIDEV_INPUTSINK*/;
                dev.HWnd = this.Handle;

                bool result = KeyboardRawInput.RegisterRawInputDevices(new KeyboardRawInput.RawInputDevice[] { dev }, 1, Marshal.SizeOf(typeof(KeyboardRawInput.RawInputDevice)));
                if (!result)
                    throw new Exception(string.Format("LastError: 0x{0:x}", Marshal.GetLastWin32Error()));

            } catch (Exception ex) {
                MessageBox.Show(ex.Message, "Error registering RawInput");
            }

            base.OnLoad(e);
        }

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == 0xFF) {
                KeyboardRawInput.RawKeyboard keyboard = new KeyboardRawInput.RawKeyboard();
                uint size = (uint)Marshal.SizeOf(keyboard);
                uint result = KeyboardRawInput.GetRawInputData(m.LParam, 0x10000003, ref keyboard, ref size, 4 + 4 + IntPtr.Size * 2);
                if (result != uint.MaxValue) {
                    string parse = string.Format("MakeCode: 0x{0:X}\r\nMessage: 0x{1:X}\r\nVKey: 0x{2:X}", keyboard.MakeCode, keyboard.Message, keyboard.VKey);
                    MessageBox.Show(parse);
                }
            }

            base.WndProc(ref m);
        }
    }
}
like image 198
Andrey Avatar answered Mar 17 '26 20:03

Andrey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!