Ejemplo n.º 1
0
        public static Thread MouseListenerThread(OnDeviceEvent onDeviceEventCallback, string deviceIdFilter = null)
        {
            Thread mouseListenerThread = new Thread(() =>
            {
                var mouse    = new RawInputReceiverWindow();
                mouse.Input += (sender, e) =>
                {
                    if (deviceIdFilter != e.Data.Header.DeviceHandle.ToString())
                    {
                        return;
                    }
                    onDeviceEventCallback(e.Data);
                };

                try
                {
                    // Register the HidUsageAndPage to watch any device.
                    RawInputDevice.RegisterDevice(HidUsageAndPage.Mouse, RawInputDeviceFlags.ExInputSink, mouse.Handle);
                    Application.Run();
                }
                finally
                {
                    RawInputDevice.UnregisterDevice(HidUsageAndPage.Mouse);
                }
            });

            mouseListenerThread.Priority = ThreadPriority.Highest;
            mouseListenerThread.Start();
            return(mouseListenerThread);
        }
Ejemplo n.º 2
0
        public static Thread JoystickListenerThread(OnDeviceEvent onDeviceEventCallback, string deviceIdFilter = null)
        {
            Thread joystickListenerThread = new Thread(() =>
            {
                var joystick = new RawInputReceiverWindow();
                RawInputData previousMessage = null;
                joystick.Input += (sender, e) =>
                {
                    if (deviceIdFilter != e.Data.Header.DeviceHandle.ToString())
                    {
                        return;
                    }
                    if (previousMessage?.ToString() == e.Data.ToString())
                    {
                        return;
                    }
                    dynamic dataObject = e.Data;
                    if (dataObject.Hid.Count > 1)
                    {
                        return;
                    }
                    previousMessage = e.Data;
                    onDeviceEventCallback(e.Data);
                };

                try
                {
                    // Register the HidUsageAndPage to watch any device.
                    RawInputDevice.RegisterDevice(HidUsageAndPage.Joystick, RawInputDeviceFlags.ExInputSink, joystick.Handle);
                    Application.Run();
                }
                finally
                {
                    RawInputDevice.UnregisterDevice(HidUsageAndPage.Joystick);
                }
            });

            joystickListenerThread.Priority = ThreadPriority.Highest;
            joystickListenerThread.Start();
            return(joystickListenerThread);
        }
Ejemplo n.º 3
0
        public static Thread KeyboardListenerThread(OnDeviceEvent onDeviceEventCallback, string deviceIdFilter = null)
        {
            Thread keyboardListenerThread = new Thread(() =>
            {
                var keyboard = new RawInputReceiverWindow();
                RawInputData previousMessage = null;
                keyboard.Input += (sender, e) =>
                {
                    if (deviceIdFilter != e.Data.Header.DeviceHandle.ToString())
                    {
                        return;
                    }
                    if (previousMessage?.ToString() == e.Data.ToString())
                    {
                        return;
                    }
                    previousMessage = e.Data;
                    onDeviceEventCallback(e.Data);
                };

                try
                {
                    // Register the HidUsageAndPage to watch any device.
                    RawInputDevice.RegisterDevice(HidUsageAndPage.Keyboard, RawInputDeviceFlags.ExInputSink, keyboard.Handle);
                    Application.Run();
                }
                finally
                {
                    RawInputDevice.UnregisterDevice(HidUsageAndPage.Keyboard);
                }
            });

            keyboardListenerThread.Priority = ThreadPriority.Highest;
            keyboardListenerThread.Start();
            return(keyboardListenerThread);
        }
Ejemplo n.º 4
0
 private void OnEventHandle(int type, int code)
 {
     OnDeviceEvent?.Invoke(type, code);
 }