Solved

Capturing mouse events anywhere in Windows

Posted on 2006-06-15
21
622 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
Comment Utility
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 50 total points
Comment Utility
0
 
LVL 7

Expert Comment

by:pradeepsudharsan
Comment Utility
0
 
LVL 7

Assisted Solution

by:pradeepsudharsan
pradeepsudharsan earned 50 total points
Comment Utility
0
 
LVL 10

Author Comment

by:imrancs
Comment Utility
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
Comment Utility
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
Comment Utility
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 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 100 total points
Comment Utility
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
Comment Utility
Thanks Idle_Mind, I'll check it tomorrow.

Imran
0
 
LVL 10

Author Comment

by:imrancs
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
Did you ever find a solution?

Bob
0
 
LVL 10

Author Comment

by:imrancs
Comment Utility
Not yet, but still looking for some solution.

Imran
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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 300 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now