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

发布于 2024-09-08 17:32:16 字数 665 浏览 2 评论 0原文

我正在重新制作 Windows 扫雷(来自 XP),他们所包含的内容是,如果您同时用鼠标左键和右键单击一个带有与该数字一样多的标志的数字,它会显示该数字周围的所有其他隐藏图块。

我很难判断何时同时按下鼠标左键和右键...我使用一对布尔值,每个按钮对应一个 OnMouseDown 和 OnMouseUp 事件,但如果这两个按钮在完全相同的时间(或非常接近)单击,则只有一个 MouseDown 事件发生,而另一个则不会...如果您单击并按住其中一个按钮,然后单击并按住另一个按钮,则代码可以正常工作。

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

编辑:

好吧,这是一个小故事,说明为什么我搞砸了(它一直有效)。

我有一台运行 Windows 7 的 MacBook Pro。对于那些不知道的人来说,MacBook Pro 有一个鼠标按钮栏,通常是左键单击,但如果您将 2 个手指向下单击,则可以右键单击,所以您可以'不能同时执行这两项操作(并且无法单击鼠标中键)。所以我正在构建我的应用程序并将其发送给我的朋友,他告诉我它不起作用,所以我发布了这个问题。我最终决定在我的另一台笔记本电脑(带有 2 个鼠标按钮的戴尔 XPS)上尝试一下......一旦它在那里工作,我就把它传递给其他朋友,他们确认它工作正常。我不知道我的第一个朋友是怎么搞砸的,但这个故事的寓意是不要买苹果的任何东西。至少这是我的道德。

I'm remaking windows Minesweeper (from XP) and something they had included was that if you click a number with as many flags as it's number with the left and right mouse button at the same time, it reveals every other hidden tile around that number.

I'm having a hard time telling when both the Left and Right mouse buttons are pressed at the exact same time... I'm using a pair of bools, one for each button with the OnMouseDown and OnMouseUp events but if the 2 buttons are clicked at the exact same time (or really close), then only one MouseDown event goes off and the other does not... If you click and hold one of the buttons then click and hold the other, the code works though.

Is there a better way to detect this kind of "dual" click?

Edit:

Alright, small story for why I messed this up (it worked all along).

I have a macbook pro running Windows 7. For those of you who don't know, the macbook pro has a single bar for a mouse button that normally left clicks, but if you place 2 fingers down it right clicks, so you can't do both (and no way to middle click). So I was building my app and sending it to my friend, he was telling me it wasn't working, so I posted this question. I finally decided to try it on my other laptop, a Dell XPS with 2 mouse buttons... Once it worked there I passed it along to other friends and they confirmed it worked. I don't know how my first friend messed it up, but moral of the story is don't buy anything Apple. At least that's the moral I got.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

被翻牌 2024-09-15 17:32:16

为左右按钮创建一个类布尔变量,默认为 false。当鼠标按下事件触发时,将变量设置为 true 并检查两者是否都为 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;
     }

Create a class boolean variable for the left and right button defaulted to false. When the mouse down event fires set the variable to true and check if both are true. When the mouse up fires set the variable to 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;
     }
苦笑流年记忆 2024-09-15 17:32:16

完整代码:

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

Complete Code:

    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;
    }
落叶缤纷 2024-09-15 17:32:16

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

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

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

您还可以查看 MSDN 示例

Another option is to use the static MouseButtons on the System.Windows.Forms.Control class

This will tell you which mouse buttons are currently pressed so that you can do something like the following:

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

You can also check out the MSDN example

坏尐絯 2024-09-15 17:32:16

我在我的点击事件中使用了以下代码。

if ((Control.MouseButtons == MouseButtons.Right) || (Control.MouseButtons == MouseButtons.Left))

当仅按下一个鼠标按钮时,“Control.MouseButton”采用“MouseButtons.None”的值,

但是当同时按下鼠标左键和右键时,“Control.MouseButton”采用“MouseButtons.Right”的值" 或 "MouseButtons.Left" 根据最先/最后按下的按钮(取决于按下左右按钮之间的时间间隔)

I got the following code to work in my Click Event.

if ((Control.MouseButtons == MouseButtons.Right) || (Control.MouseButtons == MouseButtons.Left))

When only one mouse button is being pressed the "Control.MouseButton" assumes the value of "MouseButtons.None"

But when both the left and right mouse buttons are being pressed the "Control.MouseButton" assumes the value of either "MouseButtons.Right" or "MouseButtons.Left" according to which was pressed first / last (depending on how long time it was between the left and right button being pressed)

终陌 2024-09-15 17:32:16

试试这个,

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

Try this,

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
绝情姑娘 2024-09-15 17:32:16

这是一个老问题,但我遇到了这个(碰巧也是在做扫雷克隆时)并觉得它缺少一些东西。如果您想要进行两按钮单击,但仍捕获常规的单按钮单击,则可以执行以下操作:

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 标志,以便在光标周围绘制提示方块,显示当您释放按钮时将显示哪些方块。

Kind of an old question, but I came across this (coincidentally also while doing a Minesweeper clone) and felt it was missing something. If you want to have the two-button click but still catch regular single-button clicks as well, you can do the following:

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

It moves the detection to the mouse-up rather than the mouse-down. It doesn't do anything until you release both buttons. This works almost exactly like the Windows 7 version of Minesweeper, except that the right button alone in the original operates on mouse-down. (I honestly prefer my version). There's a bit of redundancy in that the both-click case is called in two places depending on whether you release the left or right button first, but this should probably be a single-line function-call anyhow. Bonus: You can check the both_click flag from elsewhere in order to draw the hint-square around your cursor showing which squares will be revealed when you release the buttons.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文