简体   繁体   English

如何检查是否一直按住鼠标左键而不是只单击一次?

[英]How do I check if the mouse left button is pressed all the time and not clicked only once?

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

This code return one single click on the mouse left button. 此代码返回一次单击鼠标左键的操作。 But I want it to return when pressing the mouse left button without leaving the button and then moving the mouse around up,down,left,right. 但是我希望它在按下鼠标左键而不离开该按钮然后在上,下,左,右移动鼠标时返回。

How can I do it ? 我该怎么做 ?

Seems pretty straightforward to me. 对我来说似乎很简单。

bool WasMouseLeftDown()
{
    return currentState.LeftButton == ButtonState.Pressed;
}

This is simply a bool that returns true if the left mouse button is being held down in the current frame. 这只是一个布尔值,如果在当前帧中按住鼠标左键,则返回true。

I think what you really want to do is when they move the mouse while the left mouse button is down so you simply need to check both conditions: 我认为您真正想做的是当鼠标左键按下时他们移动鼠标,因此您只需要检查以下两种情况:

if (WasMouseMoved() && WasMouseLeftDown())
{
    // do something
}

I assume you already have code to check if the mouse moved (comparing the old position to the new position). 我假设您已经有代码来检查鼠标是否移动(将旧位置与新位置进行比较)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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