简体   繁体   中英

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))
     {
         this.graphics.PreferredBackBufferWidth = 800;
         this.graphics.PreferredBackBufferHeight = 600;
         this.graphics.IsFullScreen = false;
         this.graphics.ApplyChanges();
     }
     if (mouseState.LeftButton == ButtonState.Pressed)
     {
         this.graphics.PreferredBackBufferWidth = 1920;
         this.graphics.PreferredBackBufferHeight = 1080;
         this.graphics.IsFullScreen = true;
         this.graphics.ApplyChanges();
     }

     AddToCameraPosition(moveVector * amount);
 }

I added:

if (mouseState.LeftButton == ButtonState.Pressed)
{
    this.graphics.PreferredBackBufferWidth = 1920;
    this.graphics.PreferredBackBufferHeight = 1080;
    this.graphics.IsFullScreen = true;
    this.graphics.ApplyChanges();
}

I used a break point and when clicking the left mouse button it does nothing. It never enter this if block. It is getting to the if 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?

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM