开发者

Read input from USB PC Remote

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/so开发者_如何学编程ftware-to-customise-this-usb-pc-remote-control but still no answer for me. I have this remote


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


these libraries should be able to handle whatever you need for accessing/using the Remote:

  • https://github.com/mikeobrien/HidLibrary
  • http://www.usbhidnetclass.org/ (commercial)
  • http://www.codeproject.com/KB/cs/USB_HID.aspx
  • http://www.lvr.com/hidpage.htm
  • http://www.codeproject.com/KB/system/HIDAche.aspx?q=C%23+and+USB+HID+Devices

As for handling specific events/keys etc.:

Usually these are just virtual key codes - for an official list see http://msdn.microsoft.com/en-us/library/dd375731%28v=VS.85%29.aspx

There you find for example VK_VOLUME_UP VK_MEDIA_PLAY_PAUSE VK_ZOOM

Most Remotes translate to these codes to be as compatible as possible with existing software..

These codes were introduced back in the day when Windows ME (!) came out and are still in use - at least when I checked the registry of my Windows 2008 R2 !

Basically Windows translates certain VK* into WM_APPCOMMAND messages with certain codes which the applications listen to...

IF you want to map some specific key from your Remote so that Windows automatically starts an/your app like it does for example with certain remots and Mail/Browser etc.:

This magic happens via Windows Explorer which reads the mapping (either by association or direct exec) from the registry at Software\ Microsoft\ Windows\ CurrentVersion\ Explorer\ AppKey - either HKLM or HKCU.

Some links with old but as it seems still valid information:

  • http://msdn.microsoft.com/en-us/windows/hardware/gg463446.aspx
  • http://msdn.microsoft.com/en-us/windows/hardware/gg462991
  • http://msdn.microsoft.com/en-us/windows/hardware/gg463372
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜