private static void StartJoystickCapture(CancellationToken token) { var joystickGuid = Guid.Empty; var di = new DirectInput(); foreach (var device in di.GetDevices(DeviceType.Joystick, DeviceEnumerationFlags.AttachedOnly)) { joystickGuid = device.InstanceGuid; break; } _joystick = new SharpDX.DirectInput.Joystick(di, joystickGuid); _joystick.Acquire(); _currentThrottle = 0; _currentYawTrim = 0; while (!token.IsCancellationRequested) { var state = _joystick.GetCurrentState(); _currentThrottle = (1000 - GetAnalogStickValue(state.Sliders[0])) / 2; Helicopter.Command.MainThrottle = _currentThrottle; Helicopter.Command.Pitch = GetAnalogStickValue(state.Y); //Helicopter.Command.Yaw = -1 * (GetAnalogStickValue(state.X) + _currentYawTrim); //For n64 controller Helicopter.Command.Yaw = -1 * (GetAnalogStickValue(state.RotationZ) + _currentYawTrim); var yawtrimChange = GetYawTrimChange(state.Buttons); _currentYawTrim += yawtrimChange; Helicopter.Command.YawTrim = _currentYawTrim; SetConsoleDisplay(state); Thread.Sleep(Speed); } }
public Controller() { foreach (var deviceInstance in directInput.GetDevices(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(DeviceType.Joystick, DeviceEnumerationFlags.AllDevices)) { joystickGuid = deviceInstance.InstanceGuid; } } // If Joystick not found, throws an error if (joystickGuid == Guid.Empty) { MessageBox.Show("No joystick/Gamepad found."); } else { value = 0; joystick = new Joystick(directInput, joystickGuid); // Set BufferSize in order to use buffered data. joystick.Properties.BufferSize = 128; // Acquire the joystick joystick.Acquire(); } }
public void ObtainJoystick() { if (DJoystick != null) { DJoystick.Dispose(); } directInput = new DirectInput(); // Find a Joystick Guid var joystickGuid = Guid.Empty; foreach (var deviceInstance in directInput.GetDevices(DeviceType.Joystick, DeviceEnumerationFlags.AllDevices)) { joystickGuid = deviceInstance.InstanceGuid; } if (joystickGuid == Guid.Empty) { Log.LogMessage("No joystick connected"); return; } // Instantiate the joystick DJoystick = new DirectJoystick(directInput, joystickGuid); Log.LogMessage("Found Joystick with GUID: {0} Name: {1}", joystickGuid, DJoystick.Information.ProductName); // Set BufferSize in order to use buffered data. DJoystick.Properties.BufferSize = 128; // Acquire the joystick DJoystick.Acquire(); }
public void Start() { if (padConnected) { Pad.Acquire(); } }
/// <summary> /// Collect the current data from the device /// </summary> public void GetAxisData(out int x, out int y, out int rz) { x = 0; y = 0; rz = 0; // Make sure there is a valid device. if (null == m_device) { return; } // Poll the device for info. try { m_device.Poll( ); } catch (SharpDXException e) { if ((e.ResultCode == ResultCode.NotAcquired) || (e.ResultCode == ResultCode.InputLost)) { // Check to see if either the app needs to acquire the device, or // if the app lost the device to another process. try { // Acquire the device. m_device.Acquire( ); } catch (SharpDXException) { // Failed to acquire the device. This could be because the app doesn't have focus. return; // EXIT unaquired } } else { log.Error("Unexpected Poll Exception", e); return; // EXIT see ex code } } // Get the state of the device - retaining the previous state to find the lates change m_prevState = m_state; try { m_state = m_device.GetCurrentState( ); } // Catch any exceptions. None will be handled here, // any device re-aquisition will be handled above. catch (SharpDXException) { return; } x = m_state.X; y = m_state.Y; rz = m_state.RotationZ; }
public IReader Create(Identifier id) { var joystick = new SharpDX.DirectInput.Joystick(_directInput, _resolver.Resolve(id)); joystick.Properties.BufferSize = 128; joystick.Acquire(); return(new Reader(joystick)); }
public override bool AcquireJoystick(string name) { joystick = getJoyStickByNameInternal(name); if (joystick == null) { return(false); } joystick.Acquire(); joystick.Poll(); return(true); }
public SharpDX.DirectInput.Joystick AcquireJoystick(string name) { joystick = getJoyStickByName(name); if (joystick == null) { return(null); } joystick.Acquire(); joystick.Poll(); return(joystick); }
public bool Init() { // Initialize DirectInput var directInput = new DirectInput(); // Find a Joystick Guid var joystickGuid = Guid.Empty; foreach (var deviceInstance in directInput.GetDevices(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(DeviceType.Joystick, DeviceEnumerationFlags.AllDevices)) { joystickGuid = deviceInstance.InstanceGuid; } } // If Joystick not found, throws an error if (joystickGuid == Guid.Empty) { //log.MsgWriteLine(LogWindow.MsgTypes.Error, "No joystick/Gamepad found."); return(false); } // Instantiate the joystick dxJoystick = new SharpDX.DirectInput.Joystick(directInput, joystickGuid); //log.MsgWriteLine(LogWindow.MsgTypes.Normal, "Found Joystick/Gamepad with GUID: {0}", joystickGuid); // Query all suported ForceFeedback effects var allEffects = dxJoystick.GetEffects(); //foreach (var effectInfo in allEffects) //log.MsgWriteLine(LogWindow.MsgTypes.Normal, "Effect available {0}", effectInfo.Name); // Set BufferSize in order to use buffered data. dxJoystick.Properties.BufferSize = 128; // Acquire the joystick dxJoystick.Acquire(); return(true); }
public void readInput() { if (gamepadDevice == null) { return; } SharpDX.DirectInput.Joystick joystick = new SharpDX.DirectInput.Joystick(directInput, gamepadDevice.InstanceGuid); joystick.Properties.BufferSize = 128; joystick.Acquire(); while (true) { if (joystick.IsDisposed) { return; } joystick.Poll(); var datas = joystick.GetBufferedData(); foreach (var state in datas) { if (state.Offset.ToString().Equals("X")) { valueX = state.Value; Console.WriteLine(valueX); } else if (state.Offset.ToString().Equals("Y")) { valueY = state.Value; } else if (state.Offset.ToString().Equals("Buttons0")) { switch (state.Value) { case 0: isButtonClicked = false; break; case 128: isButtonClicked = true; break; } } else if (state.Offset.ToString().Equals("Z")) { valueTriggers = state.Value; } } } }
void Find_Joystick() { if (joystickGuid != Guid.Empty) { return; } foreach (var deviceInstance in directInput.GetDevices(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(DeviceType.Joystick, DeviceEnumerationFlags.AllDevices)) { joystickGuid = deviceInstance.InstanceGuid; } } // If Joystick not found, throws an error if (joystickGuid == Guid.Empty) { Console.WriteLine("No joystick/Gamepad found."); joystickEnable.Dispatcher.Invoke(() => { joystickEnable.IsChecked = false; joystickEnable.IsEnabled = false; }); } else { // Instantiate the joystick joystick = new SharpDX.DirectInput.Joystick(directInput, joystickGuid); Console.WriteLine("Found Joystick/Gamepad with GUID: {0}", joystickGuid); joystick.Properties.BufferSize = 128; // Acquire the joystick joystick.Acquire(); joystickEnable.Dispatcher.Invoke(() => { joystickEnable.IsEnabled = true; }); } }
protected void GenericControllerConnection() { #if DEBUG Console.WriteLine("Launched new thread!"); Console.WriteLine("Looking for Joystick!"); #endif var joystickGuid = Guid.Empty; foreach (var deviceInstance in directInput.GetDevices(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(DeviceType.Joystick, DeviceEnumerationFlags.AllDevices)) { joystickGuid = deviceInstance.InstanceGuid; } } if (joystickGuid == Guid.Empty) { return; } #if DEBUG Console.WriteLine("Found Joystick/Gamepad with GUID: {0}", joystickGuid); #endif joystick = new Joystick(directInput, joystickGuid); var allEffects = joystick.GetEffects(); #if DEBUG foreach (var effectInfo in allEffects) { Console.WriteLine("Effect available {0}", effectInfo.Name); } #endif joystick.Properties.BufferSize = 128; joystick.Acquire(); }
public void Start() { foreach (var port in MainV2.Comports) { foreach (var MAV in port.MAVlist) { DG.Drones.Add(new Drone() { MavState = MAV }); } } foreach (var device in directInput.GetDevices(DeviceClass.GameControl, DeviceEnumerationFlags.AttachedOnly)) { var joystick = new SharpDX.DirectInput.Joystick(directInput, device.InstanceGuid); joystick.Acquire(); joystick.Poll(); Console.WriteLine(joystick.Information.ProductName); Joysticks.Add(joystick); } if (threadrun == true) { threadrun = false; return; } new System.Threading.Thread(mainloop) { IsBackground = true }.Start(); DG.CurrentMode = DroneGroup.Mode.idle; }
public InputDeviceJoystickWindows(DirectInput di, DeviceInstance d, bool paxison) { jsi = new InputDeviceIdentity() { Instanceguid = d.InstanceGuid, Productguid = d.ProductGuid, Name = d.InstanceName.RemoveTrailingCZeros() }; axisevents = paxison; stick = new SharpDX.DirectInput.Joystick(di, d.InstanceGuid); stick.SetNotification(eventhandle); stick.Acquire(); axispresent = new bool[AxisCount]; axisvalue = Enumerable.Repeat(AxisNullValue, AxisCount).ToArray(); Capabilities c = stick.Capabilities; butstate = new bool[c.ButtonCount]; povvalue = Enumerable.Repeat(POVNotPressed, c.PovCount).ToArray(); slidercount = 0; DeviceProperties p = stick.Properties; jsi.VendorId = p.VendorId; jsi.ProductId = p.ProductId; // string s = p.PortDisplayName; System.Diagnostics.Debug.WriteLine("JOY {0} {1} but {2} pov {3}", jsi.Name, jsi.Productguid, butstate.Length, povvalue.Length); foreach (DeviceObjectInstance deviceObject in stick.GetObjects()) { if ((deviceObject.ObjectId.Flags & DeviceObjectTypeFlags.Axis) != 0) { System.Guid guid = deviceObject.ObjectType; //System.Diagnostics.Debug.WriteLine(" {0} {1} {2} {3} {4}", jsi.Name, deviceObject.UsagePage, deviceObject.Usage, deviceObject.Offset, guid.ToString()); if (guid == ObjectGuid.XAxis) { axispresent[(int)Axis.X] = true; } else if (guid == ObjectGuid.YAxis) { axispresent[(int)Axis.Y] = true; } else if (guid == ObjectGuid.ZAxis) { axispresent[(int)Axis.Z] = true; } else if (guid == ObjectGuid.RxAxis) { axispresent[(int)Axis.RX] = true; } else if (guid == ObjectGuid.RyAxis) { axispresent[(int)Axis.RY] = true; } else if (guid == ObjectGuid.RzAxis) { axispresent[(int)Axis.RZ] = true; } else if (guid == ObjectGuid.Slider) { int axisentry = (int)Axis.U + slidercount; if (axisentry < AxisCount) { axispresent[axisentry] = true; slidercount++; // must be sliders, only ones left with axis //System.Diagnostics.Debug.WriteLine("Slider " + slidercount); } } else { System.Diagnostics.Debug.WriteLine("Unknown Axis " + guid.ToString()); } ObjectProperties o = stick.GetObjectPropertiesById(deviceObject.ObjectId); o.Range = new InputRange(AxisMinRange, AxisMaxRange); } } }
public bool AcquireJoystick(Guid guid) { try { if (_joystick != null) { _joystick.Unacquire(); _joystick = null; } var dinput = new DirectInput(); foreach (DeviceInstance device in dinput.GetDevices(DeviceClass.GameControl, DeviceEnumerationFlags.AttachedOnly)) { if (device.InstanceGuid == guid) { _joystick = new SharpDX.DirectInput.Joystick(dinput, device.InstanceGuid); } } if (_joystick != null) { foreach (DeviceObjectInstance deviceObject in _joystick.GetObjects()) { //if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0) switch (deviceObject.ObjectId.Flags) { case DeviceObjectTypeFlags.Axis: case DeviceObjectTypeFlags.AbsoluteAxis: case DeviceObjectTypeFlags.RelativeAxis: var ir = _joystick.GetObjectPropertiesById(deviceObject.ObjectId); if (ir != null) { try { ir.Range = new InputRange(-100, 100); } catch (Exception ex) { MainForm.LogExceptionToFile(ex); } } break; } } _joystick.Acquire(); var cps = _joystick.Capabilities; AxisCount = cps.AxeCount; UpdateStatus(); } } catch (Exception ex) { MainForm.LogExceptionToFile(ex); return(false); } return(true); }
private void Awake() { controllers = new List <SharpDX.DirectInput.Joystick>(); var joystickGuid = Guid.Empty; var di = new DirectInput(); IList <DeviceInstance> keyboards = di.GetDevices(SharpDX.DirectInput.DeviceType.Keyboard, DeviceEnumerationFlags.AttachedOnly); for (int device = 0; device < keyboards.Count; device++) { joystickGuid = keyboards[device].InstanceGuid; controllers.Add(new Joystick(di, joystickGuid)); } IList <DeviceInstance> gamepads = di.GetDevices(SharpDX.DirectInput.DeviceType.Gamepad, DeviceEnumerationFlags.AttachedOnly); for (int device = 0; device < gamepads.Count; device++) { joystickGuid = gamepads[device].InstanceGuid; controllers.Add(new Joystick(di, joystickGuid)); } IList <DeviceInstance> joysticks = di.GetDevices(SharpDX.DirectInput.DeviceType.Joystick, DeviceEnumerationFlags.AttachedOnly); for (int device = 0; device < joysticks.Count; device++) { joystickGuid = joysticks[device].InstanceGuid; controllers.Add(new Joystick(di, joystickGuid)); } if (playerNumber <= 2) { joystick = controllers[0]; joystick.Acquire(); if (playerNumber == 1) { button1 = PlayerPrefs.GetInt("Keyboard #1" + "1", 33); button2 = PlayerPrefs.GetInt("Keyboard #1" + "2", 34); dreapta = 31; stanga = 29; jos = 30; sus = 16; } else { button1 = PlayerPrefs.GetInt("Keyboard #2" + "1", 50); button2 = PlayerPrefs.GetInt("Keyboard #2" + "2", 51); dreapta = 107; stanga = 106; jos = 109; sus = 104; } } else if (playerNumber <= controllers.Count + 1) { joystick = controllers[playerNumber - 2]; joystick.Acquire(); button1 = PlayerPrefs.GetInt(joystick.Information.ProductName + "1", 0); button2 = PlayerPrefs.GetInt(joystick.Information.ProductName + "2", 2); Debug.Log(joystick.Properties.InterfacePath); } FindObjectOfType <PlayerData>().button1[playerNumber] = button1; FindObjectOfType <PlayerData>().button2[playerNumber] = button2; if (joystick != null) { defaultAxis = joystick.GetCurrentState().X; } teamNumber.text = "Team " + (team + 1); if (playerNumber >= 10) { opposite = true; } teamNumber.gameObject.SetActive(false); selected2.gameObject.SetActive(false); selected3.gameObject.SetActive(false); char1Bg.gameObject.SetActive(false); char2Bg.gameObject.SetActive(false); char3Bg.gameObject.SetActive(false); char1 = characters[0]; char2 = characters[0]; char3 = characters[0]; char1Text.text = char1.ToString(); char2Text.text = char2.ToString(); char3Text.text = char3.ToString(); FindObjectOfType <PlayerData>().playerNames[playerNumber] = char1.ToString() + char2.ToString() + char3.ToString(); }
/// <summary>Gets a list of the attached joysticks.</summary> /// <returns>A list of joysticks.</returns> private List<DI.Joystick> GetAttachedJoysticks() { List<DI.Joystick> joysticks = new List<DI.Joystick>(); foreach(DI.DeviceInstance device in _directInput.GetDevices(DI.DeviceClass.GameControl, DI.DeviceEnumerationFlags.AttachedOnly)) { try { DI.Joystick joystick = new DI.Joystick(_directInput, device.InstanceGuid); joystick.Acquire(); IList<DI.DeviceObjectInstance> deviceObjects = joystick.GetObjects(); for(int i = 0; i < deviceObjects.Count; i++) { DI.DeviceObjectInstance deviceObjectInstance = deviceObjects[i]; if((deviceObjectInstance.ObjectId.Flags & DI.DeviceObjectTypeFlags.Axis) != 0) joystick.GetObjectPropertiesById(deviceObjectInstance.ObjectId).Range = new DI.InputRange(-1000, 1000); } joysticks.Add(joystick); } catch(SharpDXException) { } } return joysticks; }
public SharpDX.DirectInput.Joystick AcquireJoystick(string name) { joystick = getJoyStickByName(name); if (joystick == null) return null; //joystick.SetDataFormat(DeviceDataFormat.Joystick); joystick.Acquire(); System.Threading.Thread.Sleep(500); joystick.Poll(); return joystick; }