C# 如何检查鼠标左键是否双击?
在构造函数中,我做了:C# 如何检查鼠标左键是否双击?,c#,xna,xna-4.0,C#,Xna,Xna 4.0,在构造函数中,我做了: mouseState = Mouse.GetState(); var mousePosition = new Point(mouseState.X, mouseState.Y); 然后在我创建的输入法中添加: private void ProcessInput(float amount) { Vector3 moveVector = new Vector3(0, 0, 0); KeyboardState keyState = Keyboard.Get
mouseState = Mouse.GetState();
var mousePosition = new Point(mouseState.X, mouseState.Y);
然后在我创建的输入法中添加:
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);
}
我补充说:
if (mouseState.LeftButton == ButtonState.Pressed)
{
this.graphics.PreferredBackBufferWidth = 1920;
this.graphics.PreferredBackBufferHeight = 1080;
this.graphics.IsFullScreen = true;
this.graphics.ApplyChanges();
}
我使用了一个断点,当点击鼠标左键时,它什么也不做。
如果块,则永远不会输入此。
它正在到达if
,但从未进入
那么我该如何让它工作呢?
我怎样才能使鼠标左键双击而不是单击一次?首先,您必须调用
mouseState = Mouse.GetState()
在每个Update()周期中。对于您来说,这可能是ProcessInput方法的开始
第二,即使如此,您编写的代码也无法工作。现在,您的代码几乎是“只要按下左键,就将游戏切换到全屏。”XNA不是事件驱动的-没有OnClick或OnDoubleClick事件,您必须自己实现这些事件或使用可用的属性
您可能希望实现如下功能:
MouseState previousState;
MouseState currentState;
bool WasMouseLeftClick()
{
return (previousState.LeftButton == ButtonState.Pressed) && (currentState.LeftButton == ButtonState.Released);
}
然后,在ProcessInput函数中,将以下内容添加到开头:
previousState = currentState;
currentState = Mouse.GetState();
您可以使用它:
if (WasMouseLeftClick())
{
// Switch to fullscreen.
}
添加一个对双击做出反应的函数会稍微复杂一些。您必须定义两次单击之间允许的最大延迟。然后,每个周期,如果您有一个点击,您将需要检查最后一次点击发生的时间。如果比之前的延迟时间短,我们可以双击。像这样:
const float MAXDELAY = 0.5f; // seconds
DateTime previousClick;
bool WasDoubleClick()
{
return WasMouseLeftClick() // We have at least one click, and
&& (DateTime.Now - previousClick).TotalSeconds < MAXDELAY;
}
首先,你必须打电话
mouseState = Mouse.GetState()
在每个Update()周期中。对于您来说,这可能是ProcessInput方法的开始
第二,即使如此,您编写的代码也无法工作。现在,您的代码几乎是“只要按下左键,就将游戏切换到全屏。”XNA不是事件驱动的-没有OnClick或OnDoubleClick事件,您必须自己实现这些事件或使用可用的属性
您可能希望实现如下功能:
MouseState previousState;
MouseState currentState;
bool WasMouseLeftClick()
{
return (previousState.LeftButton == ButtonState.Pressed) && (currentState.LeftButton == ButtonState.Released);
}
然后,在ProcessInput函数中,将以下内容添加到开头:
previousState = currentState;
currentState = Mouse.GetState();
您可以使用它:
if (WasMouseLeftClick())
{
// Switch to fullscreen.
}
添加一个对双击做出反应的函数会稍微复杂一些。您必须定义两次单击之间允许的最大延迟。然后,每个周期,如果您有一个点击,您将需要检查最后一次点击发生的时间。如果比之前的延迟时间短,我们可以双击。像这样:
const float MAXDELAY = 0.5f; // seconds
DateTime previousClick;
bool WasDoubleClick()
{
return WasMouseLeftClick() // We have at least one click, and
&& (DateTime.Now - previousClick).TotalSeconds < MAXDELAY;
}
为什么不在ProcessInput方法中调用Mouse.GetState()?1.618您是对的。谢谢,我如何使鼠标左键双击而不是单击?为什么不在ProcessInput方法中调用mouse.GetState()?1.618您是对的。谢谢,我如何使它成为双击鼠标左键,而不是单点击?