Search code examples
c#mouseclick-eventmousedownmouse-hook

MouseHook, MouseDown and MouseClick split


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()));
}

Solution

  • 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.