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.
-
I added an answer to this question as well, I hope this is what you meant with the clicks :). – Aug 29 '11 at 21:47
-
@citroenfris lol, I actually meant to do autoclicker, I said in the post, that I don't want to actually click, I want it to automatically click, but there's nothing connected to XNA, but whatever ill just accept ur answer lol – Rich Porter Aug 29 '11 at 22:20
3 Answers
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!

- 16,329
- 10
- 48
- 66
Just call the function that clicking would call
So leftClick gets called when the user clicks, but you can call that function whenever you want
edit: Another thing you can do if you can do if you want to call mouseClick yourself (instead of leftClick) is to have your own variable sent in, and have an enum for different states you want to define.
enum CLICKS{left=0, right, middle);
public void mouseClick(){
mouseClick(-1);
}
public void mouseClick(int manualClick){
if(mouseStateCurrent.LeftButton == ButtonState.Pressed || manualClick == CLICKS.left)
leftClick();
}
public void leftClick(){
//do stuff
}
public void randomFunction(){
//doing stuff
mouseClick(CLICKS.left); //<-- will simulate a left click
}

- 748
- 10
- 18
-
Let's say the user clicks the mouse, you would call mouseClick(); Instead of waiting for the user to click, just call that function yourself. Hopefully I'm understanding your question correctly – voidraen Jul 14 '11 at 17:15
-
But the thing is, that I have an if in mouseClick method :if(mouseStateCurrent.LeftButton == ButtonState.Pressed) – Rich Porter Jul 14 '11 at 17:29
-
Well what do you do if the current state is left button? Whatever that is, make it into a function, and call that to simulate a click – voidraen Jul 14 '11 at 17:35
-
Thanks alot for helping, but I still don't understand it, where do I get the clicks from XNA? – Rich Porter Jul 14 '11 at 19:32
If you'd like to simulate real mouse pressed, you can use the SendInput Win32 method.
Ths is not as easy as the technique already offered here, but the game will not be able to distinguish whether these clicks originated from real user clicks or simulated ones.
Also check out this StackOverflow question that discussed the same issue.

- 1
- 1

- 19,570
- 21
- 109
- 218