Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++: How to catch mouse clicks wherever they happen

Tags:

c++

winapi

mouse

I am stuck with an application I am writing where I need to monitor for mouse clicks.

The clicks may happen anywhere on the screen and not inside the app window, and for each click I must pass a message (perform an action or something).

I looked around and read some suggestions like using

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

but I was unsuccessful.

Does anyone have an idea on how to implement what I need?

like image 756
stavrop Avatar asked May 30 '12 14:05

stavrop


People also ask

How do I make my mouse register all clicks?

Locking (Windows key + L) or doing Ctrl+ Alt + Del seems to "reset" the mouse detection and clicks work again.

Why is my mouse not clicking sometimes?

Change the batteries if wireless, try another USB port, reset mouse from button on bottom if available. Try the mouse in another PC to isolate if it's the mouse or Windows causing this. Try another mouse in this PC to confirm if Windows is the problem.

How many clicks do mice have?

Many standard mice have two buttons: a left button and a right button. If you are right handed, the left mouse button will be directly under your index finger when you place your hand on the mouse.


1 Answers

You need to set a mouse hook as described in MSDN.

Note that in your case the hook would need to be global. This means that you need to implement a handler function in a DLL, which will be loaded into all processes in the system which receive mouse message. Such DLL will communicate with your main application using some interprocess communication (IPC) mechanism like shared memory or via Windows messages posted (not sent) from the DLL to the main application.

You can use the source code from this CodeProject article as a guide.

Update: as per Chris' correction, I should note that above applies to "regular" mouse hook which is synchronous. Low-level hook doesn't need to be located in the DLL, but it has certain limitations which are described in the corresponding MSDN article.

like image 190
Eugene Mayevski 'Callback Avatar answered Sep 19 '22 16:09

Eugene Mayevski 'Callback