Beispiel #1
0
        private void ProcessInputCommand(ref Message message)
        {
            uint dwSize = 0;

            RawInput.GetRawInputData(message.LParam, RawInput.RawInputCommand.Input, IntPtr.Zero, ref dwSize,
                                     (uint)Marshal.SizeOf(typeof(RawInput.RAWINPUTHEADER)));

            IntPtr buffer = Marshal.AllocHGlobal((int)dwSize);

            try
            {
                if (buffer == IntPtr.Zero)
                {
                    return;
                }

                if (
                    RawInput.GetRawInputData(message.LParam, RawInput.RawInputCommand.Input, buffer, ref dwSize,
                                             (uint)Marshal.SizeOf(typeof(RawInput.RAWINPUTHEADER))) != dwSize)
                {
                    return;
                }

                RawInput.RAWINPUT raw = (RawInput.RAWINPUT)Marshal.PtrToStructure(buffer, typeof(RawInput.RAWINPUT));

                switch (raw.header.dwType)
                {
                case RawInput.RawInputType.HID:
                {
                    int offset = Marshal.SizeOf(typeof(RawInput.RAWINPUTHEADER)) + Marshal.SizeOf(typeof(RawInput.RAWHID));

                    byte[] bRawData = new byte[offset + raw.hid.dwSizeHid];
                    Marshal.Copy(buffer, bRawData, 0, bRawData.Length);

                    byte[] newArray = new byte[raw.hid.dwSizeHid];
                    Array.Copy(bRawData, offset, newArray, 0, newArray.Length);

                    string code = BitConverter.ToString(newArray);

                    if (!_useAllBytes)
                    {
                        int val = newArray[_inputByte] & _byteMask;
                        code = String.Format("{0:X2}", val);
                    }

#if TRACE
                    Trace.WriteLine(code);
#endif

                    if (_remoteHandler != null)
                    {
                        _remoteHandler(Name, code);
                    }

                    break;
                }

                case RawInput.RawInputType.Mouse:
                {
#if TRACE
                    Trace.WriteLine(String.Format("Mouse Event"));
                    Trace.WriteLine(String.Format("Buttons: {0}", raw.mouse.ulButtons));
                    Trace.WriteLine(String.Format("Raw Buttons: {0}", raw.mouse.ulRawButtons));
                    Trace.WriteLine(String.Format("Flags: {0}", raw.mouse.usFlags));
                    Trace.WriteLine(String.Format("Extra: {0}", raw.mouse.ulExtraInformation));
                    Trace.WriteLine(String.Format("Button Data: {0}", raw.mouse.buttonsStr.usButtonData));
                    Trace.WriteLine(String.Format("Button Flags: {0}", raw.mouse.buttonsStr.usButtonFlags));
                    Trace.WriteLine(String.Format("Last X: {0}", raw.mouse.lLastX));
                    Trace.WriteLine(String.Format("Last Y: {0}", raw.mouse.lLastY));
#endif
                    if (_mouseHandler != null)
                    {
                        _mouseHandler(Name, raw.mouse.lLastX, raw.mouse.lLastY, (int)raw.mouse.ulButtons);
                    }

                    break;
                }

                case RawInput.RawInputType.Keyboard:
                {
#if TRACE
                    Trace.WriteLine("Keyboard Event");
#endif

                    switch (raw.keyboard.Flags)
                    {
                    case RawInput.RawKeyboardFlags.KeyBreak:
#if TRACE
                        Trace.WriteLine(String.Format("Break: {0}", raw.keyboard.VKey));
#endif

                        if (_keyboardHandler != null)
                        {
                            _keyboardHandler(Name, raw.keyboard.VKey, true);
                        }

                        break;

                    case RawInput.RawKeyboardFlags.KeyE0:
#if TRACE
                        Trace.WriteLine(String.Format("E0: {0}", raw.keyboard.MakeCode));
#endif
                        //if (_keyboardHandler != null)
                        //  _keyboardHandler(this.Name, 0xE000 | raw.keyboard.MakeCode, true);

                        break;

                    case RawInput.RawKeyboardFlags.KeyE1:
#if TRACE
                        Trace.WriteLine("E1");
#endif
                        //if (_keyboardHandler != null)
                        //  _keyboardHandler(this.Name, 0xE100, true);

                        break;

                    case RawInput.RawKeyboardFlags.KeyMake:
#if TRACE
                        Trace.WriteLine(String.Format("Make: {0}", raw.keyboard.VKey));
#endif

                        if (_keyboardHandler != null)
                        {
                            _keyboardHandler(Name, raw.keyboard.VKey, false);
                        }

                        break;

                    case RawInput.RawKeyboardFlags.TerminalServerSetLED:
#if TRACE
                        Trace.WriteLine("TerminalServerSetLED");
#endif
                        break;

                    case RawInput.RawKeyboardFlags.TerminalServerShadow:
#if TRACE
                        Trace.WriteLine("TerminalServerShadow");
#endif
                        break;
                    }

                    break;
                }
                }
            }
            finally
            {
                Marshal.FreeHGlobal(buffer);
            }
        }
Beispiel #2
0
        private void ProcessInputCommand(ref Message message)
        {
            uint dwSize = 0;

            RawInput.GetRawInputData(message.LParam, RawInput.RawInputCommand.Input, IntPtr.Zero, ref dwSize,
                                     (uint)Marshal.SizeOf(typeof(RawInput.RAWINPUTHEADER)));

            IntPtr buffer = Marshal.AllocHGlobal((int)dwSize);

            try
            {
                if (buffer == IntPtr.Zero)
                {
                    return;
                }

                if (
                    RawInput.GetRawInputData(message.LParam, RawInput.RawInputCommand.Input, buffer, ref dwSize,
                                             (uint)Marshal.SizeOf(typeof(RawInput.RAWINPUTHEADER))) != dwSize)
                {
                    return;
                }

                RawInput.RAWINPUT raw = (RawInput.RAWINPUT)Marshal.PtrToStructure(buffer, typeof(RawInput.RAWINPUT));

                // get the name of the device that generated the input message
                string deviceName = string.Empty;
                uint   pcbSize    = 0;
                RawInput.GetRawInputDeviceInfo(raw.header.hDevice, RawInput.RIDI_DEVICENAME, IntPtr.Zero, ref pcbSize);
                if (pcbSize > 0)
                {
                    IntPtr pData = Marshal.AllocHGlobal((int)pcbSize);
                    RawInput.GetRawInputDeviceInfo(raw.header.hDevice, RawInput.RIDI_DEVICENAME, pData, ref pcbSize);
                    deviceName = Marshal.PtrToStringAnsi(pData);
                    Marshal.FreeHGlobal(pData);
                }

                Debug.WriteLine("Received Input Command ({0})", Enum.GetName(typeof(RawInput.RawInputType), raw.header.dwType));
                Debug.WriteLine("RAW HID DEVICE: {0}", deviceName);

                switch (raw.header.dwType)
                {
                case RawInput.RawInputType.HID:
                {
                    int offset = Marshal.SizeOf(typeof(RawInput.RAWINPUTHEADER)) + Marshal.SizeOf(typeof(RawInput.RAWHID));

                    byte[] bRawData = new byte[offset + raw.hid.dwSizeHid];
                    Marshal.Copy(buffer, bRawData, 0, bRawData.Length);

                    byte[] newArray = new byte[raw.hid.dwSizeHid];
                    Array.Copy(bRawData, offset, newArray, 0, newArray.Length);

                    string RawCode = BitConverter.ToString(newArray);
                    Debug.WriteLine("RAW HID DATA: {0}", RawCode);
                    // process the remote button press
                    string code = string.Empty;

                    UInt64 keyCode = 0;
                    for (int i = 1; i < newArray.Length; i++)
                    {
                        keyCode += (ulong)newArray[i] << (8 * (i - 1));
                    }
                    if (keyCode != 0)
                    {
                        string codeString = string.Format("{0:X2}{1:X6}", newArray[0], keyCode);

                        _remoteHandler(Name, codeString);

                        if (_config.DoRepeats)
                        {
                            _repeatTimer = new System.Threading.Timer(new System.Threading.TimerCallback(repeatTimerCallback),
                                                                      (object)codeString, _firstRepeatDelay, _heldRepeatDelay);
                        }
                    }
                    else
                    {
                        if (_repeatTimer != null)
                        {
                            _repeatTimer.Dispose();
                            _repeatTimer = null;
                        }
                    }

                    break;
                }
                }
            }
            finally
            {
                Marshal.FreeHGlobal(buffer);
            }
        }
Beispiel #3
0
        private void ProcMessage(ref Message m)
        {
            if (m.Msg != RawInput.WM_INPUT)
            {
                return;
            }

            uint dwSize = 0;

            RawInput.GetRawInputData(m.LParam, RawInput.RawInputCommand.Input, IntPtr.Zero, ref dwSize,
                                     (uint)Marshal.SizeOf(typeof(RawInput.RAWINPUTHEADER)));

            IntPtr buffer = Marshal.AllocHGlobal((int)dwSize);

            try
            {
                if (buffer == IntPtr.Zero)
                {
                    return;
                }

                if (
                    RawInput.GetRawInputData(m.LParam, RawInput.RawInputCommand.Input, buffer, ref dwSize,
                                             (uint)Marshal.SizeOf(typeof(RawInput.RAWINPUTHEADER))) != dwSize)
                {
                    return;
                }

                RawInput.RAWINPUT raw = (RawInput.RAWINPUT)Marshal.PtrToStructure(buffer, typeof(RawInput.RAWINPUT));

                if (raw.header.dwType == RawInput.RawInputType.HID)
                {
                    int offset = Marshal.SizeOf(typeof(RawInput.RAWINPUTHEADER)) + Marshal.SizeOf(typeof(RawInput.RAWHID));

                    byte[] bRawData = new byte[offset + raw.hid.dwSizeHid];
                    Marshal.Copy(buffer, bRawData, 0, bRawData.Length);

                    byte[] newArray = new byte[raw.hid.dwSizeHid];
                    Array.Copy(bRawData, offset, newArray, 0, newArray.Length);

                    string code = BitConverter.ToString(newArray);

                    TimeSpan timeSpan = DateTime.Now - _lastCodeTime;

                    if (!code.Equals(_lastCode, StringComparison.Ordinal) || timeSpan.Milliseconds > 250)
                    {
                        if (_remoteButtonHandler != null)
                        {
                            _remoteButtonHandler(Name, code);
                        }

                        _lastCodeTime = DateTime.Now;
                    }

                    _lastCode = code;
                }
            }
            finally
            {
                Marshal.FreeHGlobal(buffer);
            }
        }