?
Solved

Capturing mouse events anywhere in Windows

Posted on 2006-06-15
21
Medium Priority
?
648 Views
Last Modified: 2012-06-21
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
Comment
Question by:imrancs
  • 11
  • 5
  • 2
  • +3
21 Comments
 
LVL 2

Expert Comment

by:Anandavally_P
ID: 16910406
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
 
LVL 5

Assisted Solution

by:pgloor
pgloor earned 200 total points
ID: 16910419
0
 
LVL 7

Expert Comment

by:pradeepsudharsan
ID: 16910483
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Assisted Solution

by:pradeepsudharsan
pradeepsudharsan earned 200 total points
ID: 16910502
0
 
LVL 10

Author Comment

by:imrancs
ID: 16910532
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
 
LVL 10

Author Comment

by:imrancs
ID: 16910605
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
 
LVL 10

Author Comment

by:imrancs
ID: 16910611
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
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 400 total points
ID: 16911437
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
 
LVL 10

Author Comment

by:imrancs
ID: 16912264
Thanks Idle_Mind, I'll check it tomorrow.

Imran
0
 
LVL 10

Author Comment

by:imrancs
ID: 16919269
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17070119
Did you ever find a solution?

Bob
0
 
LVL 10

Author Comment

by:imrancs
ID: 17071808
Not yet, but still looking for some solution.

Imran
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17072160
I don't quite understand where you are having a problem.  What is wrong with the middle button stuff?

Bob
0
 
LVL 10

Author Comment

by:imrancs
ID: 17079218
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
 
LVL 10

Author Comment

by:imrancs
ID: 17079226
Can you please modify your code to display a message box when the middle mouse button is clicked?

Imran
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17087497
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
 
LVL 96

Accepted Solution

by:
Bob Learned earned 1200 total points
ID: 17087503
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
 
LVL 10

Author Comment

by:imrancs
ID: 17088319
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
 
LVL 10

Author Comment

by:imrancs
ID: 17088332
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17089952
My friend, that is another question, and one in which I don't have a specific answer, but maybe Idle_Mind does.

Bob
0
 
LVL 10

Author Comment

by:imrancs
ID: 17091042
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question