0

I need to measure time between clicking and how long i hold the mouse button outside the form. For example - i run the app and then click to another window (game, another app or desktop) and i need to get how long this click was.

This is part of my code:

private void button1_MouseDown(object sender, MouseEventArgs e)
{
    stopWatch.Stop();
    string elapsedTime = stopWatch.ElapsedMilliseconds.ToString();
    label2.Text = elapsedTime;
    using (StreamWriter sw = new StreamWriter(@"data.txt", true))
    {
        sw.WriteLine(elapsedTime);
        sw.WriteLine(MousePosition.X + "-" + MousePosition.Y);
        sw.Flush();
    }
    stopWatch.Reset();
    stopWatch.Start();
}

private void button1_MouseUp(object sender, MouseEventArgs e)
{
    stopWatch.Stop();
    string elapsedTime = stopWatch.ElapsedMilliseconds.ToString();
    label2.Text = elapsedTime;

    using (StreamWriter sw = new StreamWriter(@"data.txt", true))
    {
        sw.WriteLine(elapsedTime);
        sw.WriteLine(MousePosition.X + "-" + MousePosition.Y);
        sw.Flush();
    }
    stopWatch.Reset();
    stopWatch.Start();
}

But it is just for clicking on the button in the Form. I found and try some codes here and on the internet but none worked.

So please can you help me how to implement this to global mouse click? I will be very grateful for any advice or help.

elena.kim
  • 937
  • 4
  • 11
  • 21
klimic_8
  • 11
  • 3
  • https://stackoverflow.com/a/7497173/2864740 , perhaps. No idea if events are more filtered these days (and a comment indicate the API is no longer the same).. 10 years and multiple Windows versions later. There is no standard WinForms events that correspond to such global intercepts. – user2864740 Jun 08 '21 at 20:59

0 Answers0