/// <summary> /// Initializes a new instance of the XBoxControllerComponent class. /// </summary> public XBoxControllerComponent() : base("XBox Controller", "XBoxCont", "Outputs Data from a connected XBox Controller", "Firefly", "Arduino & I/O Boards") { var connectedControllers = XBoxController.GetConnectedControllers(); XBoxControllerWatcher watcher = new XBoxControllerWatcher(); watcher.ControllerConnected += (c) => Connected(c); }
public static int Connect() { try { _xBoxController = XBoxController.GetConnectedControllers().ToList(); } catch { } return(_xBoxController.Count); }
static void Main(string[] args) { XBoxControllerWatcher xbcw = new XBoxControllerWatcher(); xbcw.ControllerConnected += OnControllerConnected; xbcw.ControllerDisconnected += OnControllerDisconnected; Console.WriteLine("Press any key to exit."); while (!Console.KeyAvailable) { System.Threading.Thread.Sleep(1000); foreach (var c in XBoxController.GetConnectedControllers()) { Console.WriteLine("Controller " + c.PlayerIndex.ToString() + " Thumb Left X = " + c.ThumbLeftX.ToString() + ", Y = " + c.ThumbLeftY.ToString() + ", A = " + c.ButtonAPressed.ToString()); } } }
private async void ControllerListenerLoopAsync() { CancellationToken cancellationTokenSource = m_CancellationTokenSource.Token; XBoxController connectedController = XBoxController.GetConnectedControllers().FirstOrDefault(); while (!cancellationTokenSource.IsCancellationRequested) { await Task.Delay(20); if (connectedController.IsConnected) { StickValues newValues = new StickValues(connectedController.ThumbLeftX, connectedController.ThumbLeftY, connectedController.ThumbRightX, connectedController.ThumbRightY); if (m_LastUpdate < Environment.TickCount) { m_LastUpdate = Environment.TickCount + UpdateFrequency; ControllerUpdate?.Invoke(this, newValues); } if (newValues != m_LastValues) { m_LastValues = newValues; ControllerChanged?.Invoke(this, newValues); } CheckButton("A", connectedController.ButtonAPressed); CheckButton("B", connectedController.ButtonBPressed); CheckButton("X", connectedController.ButtonXPressed); CheckButton("Y", connectedController.ButtonYPressed); CheckButton("Back", connectedController.ButtonBackPressed); CheckButton("Start", connectedController.ButtonStartPressed); CheckButton("Up", connectedController.ButtonUpPressed); CheckButton("Down", connectedController.ButtonDownPressed); CheckButton("Left", connectedController.ButtonLeftPressed); CheckButton("Right", connectedController.ButtonRightPressed); CheckButton("LB", connectedController.ButtonShoulderLeftPressed); CheckButton("RB", connectedController.ButtonShoulderRightPressed); CheckButton("LT", connectedController.TriggerLeftPressed); CheckButton("RT", connectedController.TriggerRightPressed); CheckButton("LS", connectedController.ThumbpadLeftPressed); CheckButton("RS", connectedController.ThumbpadRightPressed); } } }
static void Main(string[] args) { XBoxControllerWatcher xbcw = new XBoxControllerWatcher(); xbcw.ControllerConnected += OnControllerConnected; xbcw.ControllerDisconnected += OnControllerDisconnected; Point defPnt = new Point(); GetCursorPos(ref defPnt); InputSimulator input = new InputSimulator(); _x = defPnt.X; _y = defPnt.Y; int clickBuffer = 0; int rightClickBuffer = 0; int pageNavBuffer = 0; while (!Console.KeyAvailable) { System.Threading.Thread.Sleep(10); foreach (var c in XBoxController.GetConnectedControllers()) { //handle cursor position _x += trueSpeed(c.ThumbLeftX); _y -= trueSpeed(c.ThumbLeftY); SetCursorPos(_x, _y); //handle single left clicks if (c.ButtonAPressed && clickBuffer == 0) { clickBuffer += 1; //DoMouseClickLeft(); input.Mouse.LeftButtonClick(); } else if (clickBuffer != 0 && clickBuffer < 12) { clickBuffer += 1; } else //if (clickBuffer > 12) { clickBuffer = 0; } //hold left click if (c.ButtonShoulderLeftPressed) { input.Mouse.LeftButtonDown(); } //handle right clicks if (c.ButtonBPressed && rightClickBuffer == 0) { rightClickBuffer += 1; //DoMouseClickRight(); input.Mouse.RightButtonClick(); } else if (rightClickBuffer != 0 && rightClickBuffer < 21) { rightClickBuffer += 1; } else //if (clickBuffer > 21) { rightClickBuffer = 0; } //handle page scrolling if (trueSpeed(c.ThumbRightY) > 2) { MouseWheelUp(); } if (trueSpeed(c.ThumbRightY) < -2) { MouseWheelDown(); } if (trueSpeed(c.ThumbRightX) > 2) { MouseWheelRight(); } if (trueSpeed(c.ThumbRightX) < -2) { MouseWheelLeft(); } //handle page navigation if (c.ButtonLeftPressed && pageNavBuffer == 0) { pageNavBuffer += 1; input.Keyboard.KeyPress(VirtualKeyCode.BROWSER_BACK); } else if (pageNavBuffer != 0 && pageNavBuffer < 12) { pageNavBuffer += 1; } else //if (clickBuffer > 12) { pageNavBuffer = 0; } if (c.ButtonRightPressed && pageNavBuffer == 0) { pageNavBuffer += 1; input.Keyboard.KeyPress(VirtualKeyCode.BROWSER_FORWARD); } else if (pageNavBuffer != 0 && pageNavBuffer < 12) { pageNavBuffer += 1; } else //if (clickBuffer > 12) { pageNavBuffer = 0; } } } }