同时检测鼠标左键和右键?

我正在重新制作Windows Minesweeper(来自XP),他们所包含的内容是,如果您同时使用鼠标左键单击带有尽可能多的标记的数字,则会显示该数字周围的所有其他隐藏的图块。

我很难说在同一时间同时按下左右鼠标按钮……我正在使用一对bool,每个按钮一个带有OnMouseDown和OnMouseUp事件但是如果是2个按钮在同一时间(或非常接近)单击,然后只有一个MouseDown事件关闭而另一个没有…如果您单击并按住其中一个按钮然后单击并按住另一个,代码可以正常工作。

有没有更好的方法来检测这种“双重”点击?

编辑:

好吧,小故事为什么我把它搞砸了(它一直都有效)。

我有一个运行Windows 7的macbook pro。对于那些不知道的人,macbook pro只有一个鼠标按钮栏,通常会留下点击,但是如果你放下2个手指就可以点击右键,这样你就可以’两者都做(并且没有中间点击)。 所以我正在构建我的应用程序并将其发送给我的朋友,他告诉我它不起作用,所以我发布了这个问题。 我终于决定在我的另一台笔记本电脑上试用它,带有2个鼠标按键的戴尔XPS ……一旦它在那里工作,我将它传递给其他朋友,他们确认它有效。 我不知道我的第一个朋友是如何搞砸了,但故事的道德是不要买任何苹果。 至少那是我得到的道德。

为左右按钮创建一个类boolean变量,默认为false。 当鼠标按下事件触发时,将变量设置为true并检查两者是否都为真。 当鼠标向上触发时将变量设置为false。

public bool m_right = false; public bool m_left = false; private void MainForm_MouseDown(object sender, MouseEventArgs e) { m_objGraphics.Clear(SystemColors.Control); if (e.Button == MouseButtons.Left) m_left = true; if (e.Button == MouseButtons.Right) m_right = true; if (m_left == false || m_right == false) return; //do something here } private void MainForm_MouseUp(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) m_left = false; if (e.Button == MouseButtons.Right) m_right = false; } 

完整代码:

  private void Form1_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) leftPressed = true; else if (e.Button == MouseButtons.Right) rightPressed = true; if (leftPressed && rightPressed) { MessageBox.Show("Hello"); // note: // the following are needed if you show a modal window on mousedown, // the modal window somehow "eats" the mouseup event, // hence not triggering the MouseUp event below leftPressed = false; rightPressed = false; } } private void Form1_MouseUp(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) leftPressed = false; else if (e.Button == MouseButtons.Right) rightPressed = false; } 

试试这个,

 Private Sub Form_Click(... , ByVal e As ystem.Windows.Forms.MouseEventArgs) If e.Button = MouseButtons.Right And e.Button = MouseButtons.Left Then MsgBox ('Right & Left code') End If 

这是一个古老的问题,但我偶然发现了这个问题(巧合的是,在做扫雷克隆时也是如此)并且觉得它遗漏了一些东西。 如果您希望单按两次按钮,但仍然可以捕获常规的单键按钮,则可以执行以下操作:

 private bool left_down; private bool right_down; private bool both_click; private void Form1_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { left_down = true; if (right_down) both_click = true; } if (e.Button == MouseButtons.Right) { right_down = true; if (left_down) both_click = true; } } private void Form1_MouseUp(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { if (!right_down) { if (both_click) //Do both-click stuff here else //Do left-click stuff here both_click = false; } left_down = false; } if (e.Button == MouseButtons.Right) { if (!left_down) { if (both_click) //Do both-click stuff here else //Do right-click stuff here both_click = false; } right_down = false; } } 

它将检测移动到鼠标向上而不是鼠标向下。 在释放两个按钮之前,它不会执行任何操作。 这几乎与扫雷的Windows 7版本完全相同,只是单独的右按钮在鼠标按下操作。 (老实说,我更喜欢我的版本)。 有一些冗余,因为双击情况在两个地方被调用,这取决于你是先释放左键还是右键,但这应该是一个单行函数调用无论如何。 额外奖励:您可以检查其他地方的both_click标志,以便在光标周围绘制提示方框,显示释放按钮时将显示哪些方块。

另一个选择是在System.Windows.Forms.Control类上使用静态MouseButtons

这将告诉您当前按下了哪些鼠标按钮,以便您可以执行以下操作:

 ((Control.MouseButtons & MouseButtons.Right) == MouseButtons.Right) && ((Control.MouseButtons & MouseButtons.Left) == MouseButtons.Left) 

您还可以查看MSDN示例