0

I am trying to have a C# program running in the background on Windows that will print "Hello!" after seeing that the user has clicked his or her mouse 10 times. But not just in the console window, anywhere on the screen.

The following event handler for click-tracking is from 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";
    }
}

But, I'm not sure how to actually use this. When I add this function anywhere, the MouseButtonEventArgs type is undefined.

What "using" statements do I need? How do I actually get this code to run properly -- do I call it once from main? What do I do to call it?

EDIT: Here is a picture showing Visual Studio not understanding MouseButtonEventArgs:

enter image description here

AlwaysQuestioning
  • 1,464
  • 4
  • 24
  • 48
  • 2
    In order to capture mouse events you would have to create a WinForms or WPF project. – WBuck Mar 30 '17 at 17:16
  • Use MouseClicke and MouseDoubleClick events instead – Akshay Mahajan Mar 30 '17 at 17:19
  • what exactly are you trying to do? – pm100 Mar 30 '17 at 17:35
  • I am trying to have a C# program running in the background that counts clicks anywhere on the screen when it is running. I don't want it to have to be within any C# Window, but rather anywhere on screen. – AlwaysQuestioning Mar 30 '17 at 17:40
  • To capture a click *anywhere* the built in events wont cut it, instead you need SetWindowsHookEx+WH_MOUSE_LL E.g. http://stackoverflow.com/questions/11607133/global-mouse-event-handler – Alex K. Mar 31 '17 at 14:20

2 Answers2

1

Initially You have to select the form and go to properties, Here you have to go events area and there is MouseClick event. Click that Mouse click. Go to Code behind window. there is the click event generated automatically. In that Form_MouseClick event you can count the number of clicks.

Initially declare a variable

int count = 0;

In method

Private void Form_MouseClick(object sender, MouseEventArgs e)
{
    count++;
    //add lable which will displays the count value
    label.Text=count.ToString();
}

I think which will helps to count the clicks in the form.

User6667769
  • 745
  • 1
  • 7
  • 25
0

I'm not entirely sure what you're trying to accomplish but.. To track user clicks I hooked up the "MouseDown" event on a form in a Windows Forms applications. From there I check click counts in the event handler.

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 
    }
}
}
WBuck
  • 5,162
  • 2
  • 25
  • 36
  • I am trying to have a C# program running in the background on Windows that will print "Hello!" after seeing that the user has clicked his or her mouse 10 times. But not just in the console window, anywhere on the screen. I'm not even sure how your solution works. – AlwaysQuestioning Mar 30 '17 at 17:43
  • What you're talking about would be Windows Hooks (WH_MOUSE). If you're not sure how my code is working then dealing with Windows Hooks might be a little bit too advanced for your current level. That being said, here is a link where you can read about using hooks. https://msdn.microsoft.com/en-us/library/windows/desktop/ms632589(v=vs.85).aspx – WBuck Mar 30 '17 at 17:52
  • Isn't there a Win32 API call called `GetAsyncKeyState`? Isn't there a way to simply use that? – AlwaysQuestioning Mar 30 '17 at 18:40