Doron Muzar Doron Muzar - 10 months ago 49
C# Question

How do i check if mouse left button was double click?

In the constructor I did:

mouseState = Mouse.GetState();
var mousePosition = new Point(mouseState.X, mouseState.Y);

Then inside the input method I created I added:

private void ProcessInput(float amount)
Vector3 moveVector = new Vector3(0, 0, 0);
KeyboardState keyState = Keyboard.GetState();
if (keyState.IsKeyDown(Keys.Up) || keyState.IsKeyDown(Keys.W))
moveVector += new Vector3(0, 0, -1);
if (keyState.IsKeyDown(Keys.Down) || keyState.IsKeyDown(Keys.S))
moveVector += new Vector3(0, 0, 1);
if (keyState.IsKeyDown(Keys.Right) || keyState.IsKeyDown(Keys.D))
moveVector += new Vector3(1, 0, 0);
if (keyState.IsKeyDown(Keys.Left) || keyState.IsKeyDown(Keys.A))
moveVector += new Vector3(-1, 0, 0);
if (keyState.IsKeyDown(Keys.Q))
moveVector += new Vector3(0, 1, 0);
if (keyState.IsKeyDown(Keys.Z))
moveVector += new Vector3(0, -1, 0);
if (keyState.IsKeyDown(Keys.Escape))
{ = 800; = 600; = false;;
if (mouseState.LeftButton == ButtonState.Pressed)
{ = 1920; = 1080; = true;;

AddToCameraPosition(moveVector * amount);

I added:

if (mouseState.LeftButton == ButtonState.Pressed)
{ = 1920; = 1080; = true;;

I used a break point and when clicking the left mouse button it does nothing.
It never enter this
It is getting to the
but never enters it.

So how do I make it work?
And how do I make also double click on the mouse left button and not single click?

Answer Source

For one, you must call

mouseState = Mouse.GetState()

in every Update() cycle. For you, that's probably at the beginning of the ProcessInput method.

Secondly, even so, the code you have written will not work. Your code, right now, says pretty much "As long as the left button is pressed, switch the game to fullscreen." XNA is not event-driven - there's no OnClick or OnDoubleClick events, you must implement these yourself or use the available properties.

You will probably want to implement a function like this:

MouseState previousState;
MouseState currentState;
bool WasMouseLeftClick()
    return (previousState.LeftButton == ButtonState.Pressed) && (currentState.LeftButton == ButtonState.Released);

and then, in your ProcessInput function, add this to the beginning:

previousState = currentState;
currentState = Mouse.GetState();

and you can use it:

if (WasMouseLeftClick())
    // Switch to fullscreen.

It will be slightly more complicated to add a function that will react to double clicks. You will have to define a maximum delay permitted between clicks. Then, every cycle, if you have a click, you will need to check when the last click happened. If it was less than the delay ago, we have a double click. Like this:

const float MAXDELAY = 0.5f; // seconds
DateTime previousClick;
bool WasDoubleClick()
   return WasMouseLeftClick() // We have at least one click, and
       && (DateTime.Now - previousClick).TotalSeconds < MAXDELAY;

Also, you will need to add this to the end of your ProcessInput: (note, you must add this only after you check for double-clicks or it will interpret all your clicks as double)

if (WasMouseLeftClick())
  previousClick = DateTime.Now;