1

I have a little problem. I tried to look at google, but it gave me nothing much. I made a little program that uses global hooks and get mouse coordinates on click. The problem is when I made a second method with MouseDown and when I just click a button, MouseDown method runs too.

So the question is: How can I split two methods and let them work only separately: when I click, only MouseClick method is triggered, when press and keep down for a while it triggers only MouseDown?

Should I use some timing or what? Just give me a tip what I should try to use.

P.S. Sorry, my English isn't good, it's not my native language.

Here is some code, that I think give the overview.

public void StartRecording()
{
    mouseListener = new MouseHookListener(new GlobalHooker());           

    mouseListener.Start();
    mouseListener.MouseClick += new MouseEventHandler(MouseClicked);
    mouseListener.MouseDown += new MouseEventHandler(MouseDown);                
}

public void MouseClicked(object sender, MouseEventArgs e)
{
    MessageBox.Show(String.Format("Mouse was pressed at  {0}, {1}",
                                  Control.MousePosition.X.ToString(),
                                  Control.MousePosition.Y.ToString()));
}

public void MouseDown(object sender, MouseEventArgs e)
{
    MessageBox.Show(String.Format("Mouse was down at  {0}, {1}",
                                  Control.MousePosition.X.ToString(),
                                  Control.MousePosition.Y.ToString()));
}
PiotrWolkowski
  • 8,408
  • 6
  • 48
  • 68
Macas
  • 560
  • 3
  • 22

3 Answers3

1

When you click the mouse both is the case. MouseClick is as far as i know both MouseDown and MouseUp, where MouseDown will be fired when you press down. And MouseUp will be fired when you release the button..

I think you will have to add some functionality to the MouseDown method which counts how long the mouse was pressed down.. That would be my solution.

Martin M
  • 463
  • 8
  • 20
  • I thought too about timing, maybe that will be best solution after all. Ty for your answer – Macas Jun 30 '14 at 12:03
1

In the MouseDown event you should store the current time and coordinates in variables that are global to the class they are in.

Then, you should use the MouseUp event to detect when the mouse button is released. You can then compare the new time and coordinates to the stored ones to see how long the button was pressed and if the mouse moved in that time, and act upon it accordingly.

Alternatively, you could also use the MouseMove event to reset the time and coordinates if required.

John Willemse
  • 6,608
  • 7
  • 31
  • 45
1

As a workaround, instead of hooking to MouseClick, try using MouseUp.

Then on MouseDown save current time in a class level variable (clickTime in the example). On MouseUp check the time again. If the time span is longer then say 3 seconds - do what you wanted to do on mouse down, otherwise - continue with mouse click.

    DateTime clickTime;
    public void MouseUp(object sender, MouseEventArgs e)
    {
        if (DateTime.Now - clickTime > someDelayTime)
        {
            // Do mouse down 
        }
        else
        {
            // Do mouse click
        }
    }

    public void MouseDown(object sender, MouseEventArgs e)
    {
        clickTime = DateTime.Now;
    }
PiotrWolkowski
  • 8,408
  • 6
  • 48
  • 68