Perform a mouse, keyboard clicks with XNA
source link: https://www.codesd.com/item/perform-a-mouse-keyboard-clicks-with-xna.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Perform a mouse, keyboard clicks with XNA
I'd like to know how can I perform for example a right mouse click, or press ENTER with XNA, is it possible? I want the XNA prog to do the action, not to check if I clicked it. Thanks.
You can use the following to make the cursor of the mouse visible (if this is necessary):
protected override void Initialize()
{
// Make mouse visible
this.IsMouseVisible = true;
base.Initialize();
}
Create the following variables, this way you can save the current and previous mouseState:
MouseState mouseStateCurrent, mouseStatePrevious;
In the Update function you can use the following:
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
{
this.Exit();
}
// Get current mouseState
mouseStateCurrent = Mouse.GetState();
// Left MouseClick
if (mouseStateCurrent.LeftButton == ButtonState.Pressed)
{
// TODO when left mousebutton clicked
}
// Right MouseClick
if (mouseStateCurrent.RightButton == ButtonState.Pressed && mouseStatePrevious.RightButton == ButtonState.Released)
{
//TODO when right mousebutton clicked
}
mouseStatePrevious = mouseStateCurrent;
// Update
base.Update(gameTime);
}
Hopefully this is of some use to you!
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK