![](/img/trans.png)
[英]Unity2d (C#) how to detect when a player clicks anywhere on the screen?
[英]How can I count number of clicks anywhere on screen in C#?
我正在尝试在Windows的后台运行一个C#程序,该程序将显示“ Hello!”。 看到用户单击了10次鼠标后。 但不仅是在控制台窗口中,还是在屏幕上的任何地方。
以下用于点击跟踪的事件处理程序来自msdn.microsoft.com:
private void OnMouseDownClickCount(object sender, MouseButtonEventArgs e) {
// Checks the number of clicks.
if (e.ClickCount == 1) {
// Single Click occurred.
lblClickCount.Content = "Single Click";
}
if (e.ClickCount == 2) {
// Double Click occurred.
lblClickCount.Content = "Double Click";
}
if (e.ClickCount >= 3) {
// Triple Click occurred.
lblClickCount.Content = "Triple Click";
}
}
但是,我不确定如何实际使用它。 当我在任何地方添加此函数时, MouseButtonEventArgs
类型都是未定义的。
我需要什么“使用”语句? 我实际上如何使此代码正常运行-是否可以从main
调用一次? 我该怎么称呼它?
编辑 :这是一张图片,显示Visual Studio无法理解MouseButtonEventArgs:
我不能完全确定您要完成什么,但是..为了跟踪用户的点击,我在Windows Forms应用程序中的表单上关联了“ MouseDown”事件。 从那里,我检查事件处理程序中的点击计数。
using System;
using System.Windows.Forms;
namespace WindowsFormsApplicationTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent( );
this.MouseDown += Form1_MouseDown;
}
private void Form1_MouseDown( object sender, MouseEventArgs e )
{
// Count clicks
}
}
}
最初,您必须选择表单并转到属性,在这里您必须转到事件区域,并且有MouseClick事件。 单击该鼠标单击。 转到窗口后面的代码。 会自动产生点击事件。 在该Form_MouseClick事件中,您可以计算点击次数。
最初声明一个变量
int count = 0;
在方法中
Private void Form_MouseClick(object sender, MouseEventArgs e)
{
count++;
//add lable which will displays the count value
label.Text=count.ToString();
}
我认为这将有助于计算表单中的点击次数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.