我希望制作一个与游戏手柄一起使用的简单程序,并且可以从程序外部控制鼠标和关键字事件。我的目标是能够在沙发上控制电脑。
我当前的代码在Update()
protected override void Update(GameTime gameTime)
{
//if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
// Exit();
var gamePadStates = Enum.GetValues(typeof (PlayerIndex)).OfType<PlayerIndex>().Select(GamePad.GetState);
var mouseState = Mouse.GetState();
var direction = Vector2.Zero;
const int speed = 5;
// Gamepad
foreach (var input in gamePadStates.Where(x => x.IsConnected))
{
if (input.IsButtonDown(Buttons.DPadDown))
direction.Y += 1;
if (input.IsButtonDown(Buttons.DPadUp))
direction.Y -= 1;
if (input.IsButtonDown(Buttons.DPadLeft))
direction.X -= 1;
if (input.IsButtonDown(Buttons.DPadRight))
direction.X += 1;
direction.X += input.ThumbSticks.Left.X;
direction.Y -= input.ThumbSticks.Left.Y;
}
var oldPos = new Vector2(mouseState.X, mouseState.Y);
if (direction != Vector2.Zero)
{
var newPos = oldPos;
direction *= speed;
newPos += direction;
//newPos.X = MathHelper.Clamp(newPos.X, 0, GraphicsDevice.DisplayMode.Width);
//newPos.Y = MathHelper.Clamp(newPos.Y, 0, GraphicsDevice.DisplayMode.Height);
Mouse.SetPosition((int)newPos.X, (int)newPos.Y);
System.Diagnostics.Debug.WriteLine("New mouse pos = {0}, {1}", newPos.X, newPos.Y);
}
base.Update(gameTime);
}
编辑: 为了发送按键,我找到了这个库