简体   繁体   English

在C#中捕获鼠标单击事件

[英]Capture mouse click event in c#

I am trying to create a simple windows form applicaiton in c# which will count the right left clicks from the mouse event. 我正在尝试使用c#创建一个简单的Windows窗体应用程序,该应用程序将计算鼠标事件的右击次数。 I have copy the following code which detect the click event: 我复制了以下代码来检测click事件:

private void mouseClick(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Right)
    {
        Trace.WriteLine("Mouse clicked");
    }
}

And in Form method I add this.MouseClick += mouseClick; 然后在Form方法中添加this.MouseClick += mouseClick; . My issue is that this function activates every time a click is performed whether is right or left click. 我的问题是,无论是右击还是左击,每次执行单击都会激活此功能。 Why is that? 这是为什么?

It is by design. 这是设计使然。 MouseClick event is raised on every click - doesn't matter was it caused by left or right button. 每次单击都会引发MouseClick事件-无关紧要是由向左还是向右按钮引起的。

In order to distinguish left button from right one in this event handler - you have to check e.Button property exactly as it was done in your code: 为了在此事件处理程序中区分左按钮和右按钮-您必须像在代码中一样检查e.Button属性:

if (e.Button == MouseButtons.Right)

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

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