Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Capturing mouse events anywhere in Windows

Posted on 2006-06-15
21
Medium Priority
?
646 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

636 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