public JoystickReader(bool axis, bool includeKeyboard = false) { if (includeKeyboard) { axis = false; } pollWaitTime = 10; warmupTime = 300; directInputList = new List <DeviceInstance>(); directInput = new DirectInput(); contrl = new SlimDX.XInput.Controller(SlimDX.XInput.UserIndex.Any); gamepads = new List <Joystick>(); state = new Dictionary <Joystick, JoyAxisState>(); lastState = new Dictionary <Joystick, JoystickState>(); if (axis) { detectionEventActiveButton = false; detectionEventActiveAxis = true; } else { detectionEventActiveButton = true; detectionEventActiveAxis = false; } keybValues = includeKeyboard; quit = false; result = null; initJoystick(); startPolling(); }
void CheckIfKeyboardGotPressed(KeyboardState ks) { if (warmupTime < 0) { var allPressed = ks.PressedKeys; foreach (var keyPressed in allPressed) { JoystickResults r = new JoystickResults(); r.Device = "Keyboard"; r.AxisButton = keyPressed.ToString(); ResultFound(r); return; } } lastKbState = ks; }
void ResultFound(JoystickResults e) { quit = true; result = e; }
void CheckIfButtonGotPressed(Joystick pad, JoystickState js) { JoyAxisState last = state[pad]; bool[] curBtns = js.GetButtons(); bool[] lastBtns; if (warmupTime < 0) { lastBtns = lastState[pad].GetButtons(); for (int i = 0; i < curBtns.Length; ++i) { if (curBtns[i] != lastBtns[i]) { JoystickResults args = new JoystickResults(); args.Device = ToDeviceString(pad); args.AxisButton = "JOY_BTN" + (i + 1).ToString(); ResultFound(args); return; } } } int[] povs = js.GetPointOfViewControllers(); for (int i = 0; i < povs.Length; ++i) { if (povs[i] > -1) { string dir = "JOY_BTN_POV" + (i + 1).ToString() + "_"; switch (povs[i]) { case 0: dir += "U"; break; case 4500: dir += "UR"; break; case 9000: dir += "R"; break; case 13500: dir += "DR"; break; case 18000: dir += "D"; break; case 22500: dir += "DL"; break; case 27000: dir += "L"; break; case 31500: dir += "UL"; break; } JoystickResults args = new JoystickResults(); args.Device = ToDeviceString(pad); args.AxisButton = dir; ResultFound(args); return; } } }
void CheckIfAxisPassedThreshhold(Joystick pad, JoyAxisState current) { JoyAxisState last = state[pad]; int xDiff = last.x - current.x; int yDiff = last.y - current.y; int zDiff = last.z - current.z; int xrDiff = last.xr - current.xr; int yrDiff = last.yr - current.yr; int zrDiff = last.zr - current.zr; int s1rDiff = last.s1r - current.s1r; int s2rDiff = last.s2r - current.s2r; if (xDiff < 0) { xDiff *= -1; } if (yDiff < 0) { yDiff *= -1; } if (zDiff < 0) { yDiff *= -1; } if (xrDiff < 0) { xrDiff *= -1; } if (yrDiff < 0) { yrDiff *= -1; } if (zrDiff < 0) { zrDiff *= -1; } if (s1rDiff < 0) { s1rDiff *= -1; } if (s2rDiff < 0) { s2rDiff *= -1; } JoystickResults args = new JoystickResults(); args.Device = ToDeviceString(pad); args.AxisButton = "JOY_"; if (xDiff > axisThreshold && last.x > 10 && current.x > 10) { Console.WriteLine(last.x.ToString() + " " + current.x.ToString()); args.AxisButton += "X"; if (warmupTime < 0) { ResultFound(args); } } else if (yDiff > axisThreshold && last.y > 10 && current.y > 10) { Console.WriteLine(last.y.ToString() + " " + current.y.ToString()); args.AxisButton += "Y"; if (warmupTime < 0) { ResultFound(args); } } else if (zDiff > axisThreshold && last.z > 10 && current.z > 10) { Console.WriteLine(last.z.ToString() + " " + current.z.ToString()); args.AxisButton += "Z"; if (warmupTime < 0) { ResultFound(args); } } else if (xrDiff > axisThreshold && last.xr > 10 && current.xr > 10) { Console.WriteLine(last.xr.ToString() + " " + current.xr.ToString()); args.AxisButton += "RX"; if (warmupTime < 0) { ResultFound(args); } } else if (yrDiff > axisThreshold && last.yr > 10 && current.yr > 10) { Console.WriteLine(last.yr.ToString() + " " + current.yr.ToString()); args.AxisButton += "RY"; if (warmupTime < 0) { ResultFound(args); } } else if (zrDiff > axisThreshold && last.zr > 10 && current.zr > 10) { Console.WriteLine(last.zr.ToString() + " " + current.zr.ToString()); args.AxisButton += "RZ"; if (warmupTime < 0) { ResultFound(args); } } else if (s1rDiff > axisThreshold && last.s1r > 10 && current.s1r > 10) { Console.WriteLine(last.s1r.ToString() + " " + current.s1r.ToString()); args.AxisButton += "SLIDER1"; if (warmupTime < 0) { ResultFound(args); } } else if (s2rDiff > axisThreshold && last.s2r > 10 && current.s2r > 10) { Console.WriteLine(last.s2r.ToString() + " " + current.s2r.ToString()); args.AxisButton += "SLIDER2"; if (warmupTime < 0) { ResultFound(args); } } }