private void ToggleButton_Click(object sender, RoutedEventArgs e) { var model = (MainWindowModel)DataContext; if (model.ShouldRun == true) { var devIdAt = vJoyDeviceInput.Items[model.DeviceIdIndex]; var deviceId = Convert.ToUInt32(devIdAt); var manualWidth = 0; var manualHeight = 0; try { var cfg = new M2JConfig { VjoyDevId = deviceId, InvertX = model.InvertX, InvertY = model.InvertY, AutoCenter = model.AutoCenter, AutoSize = model.AutoScreenSize, ManualWidth = manualWidth, ManualHeight = manualHeight, LeftJoy = model.LeftJoy, SenseX = (double)XSensePresc.Value, SenseY = (double)YSensePresc.Value }; PrintMessage("---------------------------------------"); PrintMessage($"Acquiring device with properties: {cfg}"); _handler = new MouseToJoystickHandler(cfg); model.SettingsEnabled = false; } catch (Exception err) { this.ShowModalMessageExternal("Error", err.Message); PrintMessage($"{err}"); model.ShouldRun = false; model.SettingsEnabled = true; StartBtn.IsChecked = false; } } else { if (_handler != null) { _handler.Dispose(); _handler = null; } model.SettingsEnabled = true; } StartBtn.Content = StartBtn.IsChecked != null && (bool)StartBtn.IsChecked ? "Stop" : "Start"; }
public MouseToJoystickHandler(M2JConfig cfgObj) { this._id = cfgObj.VjoyDevId; this._invertX = cfgObj.InvertX ? -1 : 1; this._invertY = cfgObj.InvertY ? -1 : 1; this._senseX = cfgObj.SenseX == 0 ? 0.1 : cfgObj.SenseX; this._senseY = cfgObj.SenseY == 0 ? 0.1 : cfgObj.SenseY; this._autoCenter = cfgObj.AutoCenter; this._autoSize = cfgObj.AutoSize; this._manualWidth = cfgObj.ManualWidth; this._manualHeight = cfgObj.ManualHeight; this._leftJoy = cfgObj.LeftJoy; _joystick = new vJoy(); // Make sure driver is enabled if (!_joystick.vJoyEnabled()) { throw new InvalidOperationException("vJoy driver not enabled: Failed Getting vJoy attributes"); } // Make sure we can get the joystick VjdStat status = _joystick.GetVJDStatus(_id); switch (status) { case VjdStat.VJD_STAT_OWN: case VjdStat.VJD_STAT_FREE: break; case VjdStat.VJD_STAT_BUSY: throw new InvalidOperationException("vJoy device is already owned by another feeder"); case VjdStat.VJD_STAT_MISS: throw new InvalidOperationException("vJoy device is not installed or is disabled"); default: throw new Exception("vJoy device general error"); } ; if (!this._joystick.AcquireVJD(this._id)) { throw new Exception("Failed to acquire vJoy device"); } if (!this._joystick.ResetVJD(this._id)) { throw new Exception("Failed to reset vJoy device"); } if (_leftJoy) { if (!this._joystick.GetVJDAxisMax(this._id, HID_USAGES.HID_USAGE_X, ref this._axisMax)) { throw new Exception("Failed to get vJoy axis max"); } if (!this._joystick.GetVJDAxisMin(this._id, HID_USAGES.HID_USAGE_X, ref this._axisMin)) { throw new Exception("Failed to get vJoy axis min"); } } else { if (!this._joystick.GetVJDAxisMax(this._id, HID_USAGES.HID_USAGE_RX, ref this._axisMax)) { throw new Exception("Failed to get vJoy axis max"); } if (!this._joystick.GetVJDAxisMin(this._id, HID_USAGES.HID_USAGE_RX, ref this._axisMin)) { throw new Exception("Failed to get vJoy axis min"); } } this._axisMid = _axisMax - (_axisMax - _axisMin) / 2; // Register for mouse and keyboard events _eventHooker = Hook.GlobalEvents(); _eventHooker.MouseMove += HandleMouseMove; _eventHooker.MouseDown += HandleMouseDown; _eventHooker.MouseUp += HandleMouseUp; }