代码之家  ›  专栏  ›  技术社区  ›  Corey Ogburn

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

  •  4
  • Corey Ogburn  · 技术社区  · 15 年前

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

    编辑:

    好吧,小故事,为什么我搞砸了这个(它一直工作)。

    我有一台运行Windows7的MacBookPro。对于那些不知道的人来说,MacBookPro有一个单独的鼠标键条,通常是左键点击,但是如果你把两个手指放下来右键点击,你就不能同时做这两件事(而且没有办法中键点击)。所以我在构建我的应用程序并发送给我的朋友,他告诉我它不起作用,所以我发布了这个问题。我最后决定在我的另一台笔记本电脑上试用,一台带有两个鼠标按钮的戴尔XPS。。。一旦它在那里工作,我把它传给其他朋友,他们证实了它的工作。我不知道我的第一个朋友是怎么搞砸的,但故事的寓意是不要买任何苹果的东西。至少这是我的寓意。

    6 回复  |  直到 6 年前
        1
  •  8
  •   buckbova    15 年前

    为默认为false的左右按钮创建一个类布尔变量。当mouse down事件触发时,将变量设置为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;
         }
    
        2
  •  2
  •   Michael Buen    15 年前

    完整代码:

        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;
        }
    
        3
  •  2
  •   James    8 年前

    另一种选择是使用静态 鼠标按钮 System.Windows.Forms.Control

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

    您也可以查看 MSDN example

        4
  •  1
  •   Ola Ström Jakkra    6 年前

    我在Click事件中使用了以下代码。

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

    当只按下一个鼠标按钮时,“Control.MouseButton”假定值为“MouseButtons.None”

    但是,当同时按下鼠标左键和右键时,“Control.MouseButton”会假定“MouseButtons.right”或“MouseButtons.left”的值,这取决于按下鼠标左键和右键之间的时间长短

        5
  •  0
  •   Wassim AZIRAR    15 年前

    试试这个,

    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
    
        6
  •  0
  •   Darrel Hoffman    11 年前

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

    它将检测向上移动鼠标,而不是向下移动鼠标。它不会做任何事,直到你释放两个按钮。它的工作原理几乎与Windows7版本的扫雷舰完全相同,只是在最初的版本中只有右键在鼠标下键时起作用(我真的更喜欢我的版本)。有一点冗余,根据您是先释放左键还是右键,在两个位置调用both click case,但无论如何,这应该是一个单行函数调用。奖励:您可以查看 both_click 从别处标记,以便在光标周围绘制提示方块,显示释放按钮时将显示哪些方块。