private void M_GlobalHook_KeyUp(object sender, KeyEventArgs e) { if (e.KeyCode == _forwardKey) { SipUp?.Invoke(this, null); e.Handled = true; } else if (e.KeyCode == _selectKey) { PuffUp?.Invoke(this, null); e.Handled = true; } }
public void Start() { _stopped = false; _thread = new Thread(async() => { while (!_stopped) { try { var directInput = new DirectInput(); // Find a Joystick Guid var joystickGuid = Guid.Empty; foreach (var deviceInstance in directInput.GetDevices(SharpDX.DirectInput.DeviceType.Gamepad, DeviceEnumerationFlags.AllDevices)) { joystickGuid = deviceInstance.InstanceGuid; } // If Gamepad not found, look for a Joystick if (joystickGuid == Guid.Empty) { foreach (var deviceInstance in directInput.GetDevices(SharpDX.DirectInput.DeviceType.Joystick, DeviceEnumerationFlags.AllDevices)) { joystickGuid = deviceInstance.InstanceGuid; } } // If Joystick not found, throws an error if (joystickGuid == Guid.Empty) { throw new Exception("No joystick/Gamepad found."); } // Instantiate the joystick var joystick = new Joystick(directInput, joystickGuid); Console.WriteLine("Found Joystick/Gamepad with GUID: {0}", joystickGuid); // Query all suported ForceFeedback effects var allEffects = joystick.GetEffects(); foreach (var effectInfo in allEffects) { Console.WriteLine("Effect available {0}", effectInfo.Name); } // Set BufferSize in order to use buffered data. joystick.Properties.BufferSize = 128; // Acquire the joystick joystick.Acquire(); while (!_stopped) { joystick.Poll(); var datas = joystick.GetBufferedData(); foreach (var state in datas) { if (state.RawOffset > 10) { switch (state.Offset) { case JoystickOffset.Buttons0: if (state.Value == 0) { SipUp?.Invoke(this, null); } else { SipDown?.Invoke(this, null); } break; case JoystickOffset.Buttons1: if (state.Value == 0) { PuffUp?.Invoke(this, null); } else { PuffDown?.Invoke(this, null); } break; } } } Thread.Sleep(30); } } catch { } await Task.Delay(15000); } }) { Priority = ThreadPriority.Lowest, IsBackground = true }; _thread.Start(); // Poll events from joystick }