public void ParseKeyboardInput(ref InputState NewInput) { //gets keyboard input KeyboardState Keyboard = Input.GetKeyboardState(); if (Keyboard != null) { if (Keyboard[Key.UpArrow]) NewInput.ThumbLY = short.MaxValue; if (Keyboard[Key.LeftArrow]) NewInput.ThumbLX = short.MinValue; if (Keyboard[Key.DownArrow]) NewInput.ThumbLY = short.MinValue; if (Keyboard[Key.RightArrow]) NewInput.ThumbLX = short.MaxValue; if (Keyboard[Key.A]) NewInput.Buttons |= Buttons.Back; if (Keyboard[Key.S]) NewInput.Buttons |= Buttons.Start; if (Keyboard[Key.I]) NewInput.AnalogButtons[(int)AnalogButtons.Y] = 0xFF; if (Keyboard[Key.J]) NewInput.AnalogButtons[(int)AnalogButtons.X] = 0xFF; if (Keyboard[Key.K]) NewInput.AnalogButtons[(int)AnalogButtons.A] = 0xFF; if (Keyboard[Key.L]) NewInput.AnalogButtons[(int)AnalogButtons.B] = 0xFF; if (Keyboard[Key.U]) NewInput.AnalogButtons[(int)AnalogButtons.White] = 0xFF; if (Keyboard[Key.O]) NewInput.AnalogButtons[(int)AnalogButtons.Black] = 0xFF; if (Keyboard[Key.Escape]) { Xbox.Gamepad.OverrideControllers(false); running = false; } } }
/// <summary> /// Compares the differences between two states and determines if they have changed. /// </summary> /// <param name="oldState"></param> /// <param name="newState"></param> /// <returns></returns> bool StateChanged(InputState oldState, InputState newState) { for (int i = 0; i < 8; i++) if (oldState.AnalogButtons[i] != newState.AnalogButtons[i]) return true; if (oldState.Buttons != newState.Buttons || oldState.ThumbRX != newState.ThumbRX || oldState.ThumbRY != newState.ThumbRY || oldState.ThumbLX != newState.ThumbLX || oldState.ThumbLY != newState.ThumbLY) return true; return false; }
/// <summary> /// Sends gamepad input to a specified controller port. /// *** A controller must be plugged into the specified port for this to work. *** /// You are also responsible for the polling frequency. /// </summary> /// <param name="port">Controller port. [0-3]</param> /// <param name="input">Gamepad input.</param> public void SetState(uint port, InputState input) { if (port > 3) throw new Exception("Invalid controller port specified. Valid ports are within the range of [0-3]."); // only update if state has changed...otherwise its just a waste of bandwidth ;p if (StateChanged(PreviousState, input)) { // get our XINPUT_STATE address uint inputState = XboxHistory.Gamepad.StateBufferAddress + port * 32; // indicate a changed gamepad state PacketNumber++; //convert pad to byte array byte[] gamepadData = new byte[22]; using (var pad = new BinaryWriter(new System.IO.MemoryStream(gamepadData))) { pad.Write(PacketNumber); pad.Write((ushort)input.Buttons); pad.Write(BitConverter.ToUInt64(input.AnalogButtons, 0)); pad.Write((short)input.ThumbLX); pad.Write((short)input.ThumbLY); pad.Write((short)input.ThumbRX); pad.Write((short)input.ThumbRY); } // store new state Xbox.SetMemory(inputState, gamepadData); // replace old with new PreviousState = input; } }
void XCE_Shown(object sender, System.EventArgs e) { try { this.WindowState = FormWindowState.Normal; System.Threading.Thread.Sleep(1000); Prompt.Show(); while (!Prompt.IsDisposed) // waits for it to be disposed { System.Threading.Thread.Sleep(1); Application.DoEvents(); } Input = new Input(this); Xbox.Connect(Prompt.DebugName); Xbox.Gamepad.InitializeControllerHook(); Xbox.Gamepad.OverrideControllers(true); while (running) { NewInput = new InputState(); //DateTime before = DateTime.Now; ParseKeyboardInput(ref NewInput); #region Mouse Input MouseState Mouse = Input.GetMouseState(); if (Mouse.GetMouseButtons()[0] > 0) // left click NewInput.AnalogButtons[(int)AnalogButtons.RightTrigger] = 0xFF; if (Mouse.GetMouseButtons()[1] > 0) // right click NewInput.AnalogButtons[(int)AnalogButtons.LeftTrigger] = 0xFF; if (Mouse.GetMouseButtons()[2] > 0) // middle click NewInput.Buttons |= Buttons.RightThumb; /* // get mouse input with separate function...we need to capture constantly instead of 30fps NewInput.ThumbRX = (short)(Mouse.X * 0x7FF); NewInput.ThumbRY = (short)(-Mouse.Y * 0x7FF); //NewInput.ThumbRX = (short)(((float)short.MaxValue * (Mouse.X / 2)) / 4); //NewInput.ThumbRY = (short)(-((float)short.MaxValue * (Mouse.Y / 2) / 4)); */ #endregion //int PollsPerSecond = 30; //System.Threading.Thread.Sleep(1000 / PollsPerSecond); System.Threading.Thread.Sleep(1); Xbox.Gamepad.SetState(0, NewInput); Application.DoEvents(); } } catch { Hide(); MessageBox.Show("You slipped one past the goalie, don't let it happen again ;P", "Unhandled Exception", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { Close(); } }
public void AssignState(InputState state) { this.Buttons = state.Buttons; this.AnalogButtons = state.AnalogButtons; this.ThumbLX = state.ThumbLX; this.ThumbLY = state.ThumbLY; this.ThumbRX = state.ThumbRX; this.ThumbRY = state.ThumbRY; }