# Sample Code C#/XNA : Mouse Control Tip



## Kreij (Apr 12, 2008)

When you want to update your game logic in XNA you do it in the Update method (which is overridden). This is the ideal spot to check for mouse events, like left click, right click or whatever. XNA uses a class called "Mouse" which contains a "GetState" method that returns the state of the mouse into a "MouseState" type variable.

The problem is that the Update method is called so freakin' fast (this is a good thing because it is part of your render loop) that it is almost impossible to click the mouse fast enough to get it to see only one click.

There is a pretty easy workaround for simple cases.

Create two MouseState variables.

```
MouseState mouseStateCurrent, mouseStatePrevious;
```

In the Update method check for conditions in a logical "and" statement.

```
[color=blue]protected override void[/color] Update([color=teal]GameTime[/color] gameTime)
{
    mouseStateCurrent = [color=teal]Mouse[/color].GetState();

    if (mouseStateCurrent.LeftButton = [color=teal]ButtonState[/color].Pressed &&
        mouseStatePrevious.LeftButton = [color=teal]ButtonState[/color].Released)
    {
        // Do your mouse state logic here
    }

    mouseStatePrevious = mouseStateCurrent;

    [color=blue]base[/color].Update(gameTime);
}
```

Basically what this will do is only run the logic when it sees that the current mouse state is different than the previous state. This prevents it from running the logic in the event you are holding down the mouse button. Since the Update method runs so fast, it appears to the program that you are holding down the button even when you are just clicking, and this prevents flickering and odd things from happening.

Do note however, this will prevent your logic from running when you are dragging the mouse. If you want to be able to drag the mouse around, the logic will have to change.
You would need to also check for some other condition to have changed. For example, the mouse has moved far enough away from it's initial position that the logic needs to be run again.

If anyone would like to see an example of something like that, just let me know.

Have fun coding!


----------



## Oliver_FF (Apr 13, 2008)

Nice one! Myself and several friends are planning a large game in XNA after our university exams are out - should be fun XD


----------



## Kreij (Apr 13, 2008)

Have fun !

I'm wrting a little 2D, AI path-finding simulation and I wanted the user to be able to change the tiles to different types (open, clostly, blocked) by clicking on them with the different mouse buttons before the simulation starts.

Once I finish this I think I will write a 3D one.


----------



## Xietep (Apr 11, 2012)

Ah, I came here, because I was curious if you could possibly click the mouse so fast that the click wouldn't be picked up by a mouseState. I guess that could never happen?


----------



## Kreij (Apr 11, 2012)

I suppose that anything is possible, but it shouldn't happen. When you click the mouse, the input is stored in a buffer (you can see this in the explorer when the computer is busy and there is a slight delay between a mouse click and the OS's response).

The update method (loop) for a game runs very fast and would check the buffer every time you request it to do so (query the mouse state), but you could potentially have so much happening in the update loop that you could experience mouse lag, especially if your game is running on a single thread. If that were to occur, you would then be looking at optimization of existing code or pushing certain functionality off to separate threads.

Quite the zombie thread. I forgot I even wrote this one 4 years ago. lol


----------



## Xietep (Apr 11, 2012)

Haha, thanks for the quick reply. I was debating whether or not I should resurrect this one or not, but I figured if I found it, someone else looking for an answer might, too. And it's nice for all the answers being on one thread!

Was surprised to see you were the one to respond, too! I guess you've been around a while. I know who to go to for questions now


----------



## Kreij (Apr 11, 2012)

Always happy to help. 

Kinda funny that you kicked this one up as I haven't done anything in XNA for awhile and was just pondering a game idea today.

Oh and btw ... Welcome to TPU !!


----------



## FordGT90Concept (Apr 11, 2012)

I know my code that uses user32.dll (mouse_event) can be ignored by games if it happens too quickly (or just completely outright ignore it like Fallout New Vegas).  At the same time, it might be merging multiple clicks into a single event.


----------



## Kreij (Apr 11, 2012)

But that's an app/utility outside of the game using PInvoke, isn't it Ford?
I've had weird things happen in managed code when calling unmanaged functions.


----------

