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
Solved

Capturing mouse events anywhere in Windows

Posted on 2006-06-15
21
628 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 50 total points
ID: 16910419
0
 
LVL 7

Expert Comment

by:pradeepsudharsan
ID: 16910483
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 7

Assisted Solution

by:pradeepsudharsan
pradeepsudharsan earned 50 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 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 100 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 300 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

808 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