• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 651
  • Last Modified:

Capturing mouse events anywhere in Windows

Hi,

How can we capture the mouse events (specially mouse middle click) clicked anywhere in the Windows using C#/VB.NET?

I am using VS .NET 2003.

Thanks,
Imran
0
imrancs
Asked:
imrancs
  • 11
  • 5
  • 2
  • +3
4 Solutions
 
Anandavally_PCommented:
Try this:

You can write code in the following code in the mouse_click event of the form

 private void Form1_MouseClick(object sender, MouseEventArgs e)
 {
            if (e.Button == MouseButtons.Middle)
                MessageBox.Show("middle button is pressed");
            else if (e.Button == MouseButtons.Left)
                MessageBox.Show("left button is pressed");
            else if (e.Button == MouseButtons.Right)
                MessageBox.Show("right button is pressed");
}

This way you can check the type of mouse button clicked.

Thanks,
Anandavally P
0
 
pradeepsudharsanCommented:
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
imrancsAuthor Commented:
Guys, thanks for your help.

Anandavally P: I don't want to capture the middle mouse click only on a Form but anywhere in the Windows, it could be IE, Word, or any other text area. any how thanks for your help.

pgloor, and pradeepsudharsan; and looking into your referred links and get back to you soon after checking.

Imran
0
 
imrancsAuthor Commented:
Most of link posted above are for capturing the mouse events on the form.

Can you please send a specific link that handles the middle mouse click event when the wheel is click anywhere in any application not only on the form?

Thanks,
Imran
0
 
imrancsAuthor Commented:
Most of the links posted above are for capturing the mouse events on the form.

Can you please send a specific link that handles the middle mouse click event when the wheel is click anywhere in any application not only on the form?

Thanks,
Imran
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You need to hook with WH_MOUSE_LL flag to get global mouse messages...

See Bob's (TheLearnedOne) example here:
http://www.experts-exchange.com/Programming/Programming_Languages/C_Sharp/Q_21475143.html
0
 
imrancsAuthor Commented:
Thanks Idle_Mind, I'll check it tomorrow.

Imran
0
 
imrancsAuthor Commented:
Idle_Mind, I checked the above code. It's working fine for all key and mouse clicks. But the problem is with the middle mouse click. It's raises the following events when the middle mouse button is clicked (from the log of Bob's app):

KeyDown       - LControlKey
KeyDown       - C
KeyPress       - 
KeyUp       - C
KeyUp       - LControlKey.

Please advice.

Thanks,
Imran
0
 
Bob LearnedCommented:
Did you ever find a solution?

Bob
0
 
imrancsAuthor Commented:
Not yet, but still looking for some solution.

Imran
0
 
Bob LearnedCommented:
I don't quite understand where you are having a problem.  What is wrong with the middle button stuff?

Bob
0
 
imrancsAuthor Commented:
When I click the middle mouse button (just once) following events are fired:

KeyDown      - LControlKey
KeyDown      - C
KeyPress      - 
KeyUp      - C
KeyUp      - LControlKey.

So, how can I detect that middle mouse button is clicked?

Imran
0
 
imrancsAuthor Commented:
Can you please modify your code to display a message box when the middle mouse button is clicked?

Imran
0
 
Bob LearnedCommented:
Imran, I don't have a middle button to test, but I did notice this block:

 switch (wParam)
        {
          case WM_LBUTTONDOWN:
            //case WM_LBUTTONUP:
            //case WM_LBUTTONDBLCLK:
            button=MouseButtons.Left;
            break;
          case WM_RBUTTONDOWN:
            //case WM_RBUTTONUP:
            //case WM_RBUTTONDBLCLK:
            button=MouseButtons.Right;
            break;
        }

It's not handling the middle mouse button.  
0
 
Bob LearnedCommented:
switch (wParam)
       {
         case WM_LBUTTONDOWN:
           case WM_LBUTTONUP:
           case WM_LBUTTONDBLCLK:
           button=MouseButtons.Left;
           break;
         case WM_RBUTTONDOWN:
           case WM_RBUTTONUP:
           case WM_RBUTTONDBLCLK:
           button=MouseButtons.Right;
           break;
         case WM_MBUTTONDOWN:
           case WM_MBUTTONUP:
           case WM_MBUTTONDBLCLK:
           button=MouseButtons.Middle;
           break;
       }

Bob
0
 
imrancsAuthor Commented:
Thanks a lot Bob, it works.

I made the following changes and its working fine now.

actHook.OnMouseActivity+=new MouseEventHandler(MouseMoved);
//actHook.KeyDown+=new KeyEventHandler(MyKeyDown);
//actHook.KeyPress+=new KeyPressEventHandler(MyKeyPress);
//actHook.KeyUp+=new KeyEventHandler(MyKeyUp);

//case WM_MBUTTONDOWN:
case WM_MBUTTONUP:
//case WM_MBUTTONDBLCLK:
button=MouseButtons.Middle;
break;

Imran
0
 
imrancsAuthor Commented:
Another question [I can start a new thread if you want or I got the answer here for this question I'll post a new question for granting you the points]:

How I can get the text on which the Middle mouse button is clicked?

Thanks
Imran
0
 
Bob LearnedCommented:
My friend, that is another question, and one in which I don't have a specific answer, but maybe Idle_Mind does.

Bob
0
 
imrancsAuthor Commented:
Yes I know it's another question that is why I mentioned "[I can start a new thread if you want or I got the answer here for this question I'll post a new question for granting you the points]:".

Any way thanks for your help.

I'll post a new question for this.

Imran
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

  • 11
  • 5
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now