0

everyone. Now, I'm developing desktop window apps with c++ mfc. I wanna get the mouse move and down event on the desktop background. Why I want these, this app requires all windows move and resize event, and also mouse position. After so many googling, I don't search things as a right solution. Someone suggests that global mouse hooks is helpful, but I don't really know how to use this. What is your idea about this? Please help me to find a right solution.

Best Regards Falcon

falcon.guru
  • 33
  • 1
  • 4

1 Answers1

0

You're looking for the windows low level global input hook api SetWindowsHookEx

You can find more information here: https://msdn.microsoft.com/en-us/library/windows/desktop/ms644990(v=vs.85).aspx

Specifically, you're looking to use the "low level" mouse hook like so:

HHOOK mousehook = SetWindowsHookEx(WH_MOUSE_LL, MouseHookProc, NULL, 0);

You'll need to use this with a windows message queue per this link:

Why must SetWindowsHookEx be used with a windows message queue

The low-level hooks, WH_KEYBOARD_LL and WH_MOUSE_LL are different from all the other hooks. They don't require a DLL to be injected into the target process. Instead, Windows calls your hook callback directly, inside your own process. To make that work, a message loop is required. There is no other mechanism for Windows to make callbacks on your main thread, the callback can only occur when you've called Get/PeekMessage() so that Windows is in control.

A global hook like WH_KEYBOARD is very different. It requires a DLL and the callback occurs within the process that processes the keyboard message. You need some kind of inter-process communication to let your own program be aware of this. Named pipes are the usual choice. Which otherwise of course requires that this injected process pumps a message loop. It wouldn't get keyboard messages otherwise.

Favor a low-level hook, they are much easier to get going. But do pump or it won't work. And beware of timeouts, if you're not responsive enough then Windows will kill your hook without notice.

Community
  • 1
  • 1
Josh
  • 12,602
  • 2
  • 41
  • 47
  • Hello, Josh Thanks for your answer. I just implemented global mouse hook and I need one thing more. Can you help me? – falcon.guru Aug 23 '17 at 06:57
  • generally on stackoverflow, you ask one question per thread (which this does). when someone answers, mark it answered. if you have a second question, create a new thread. thanks! – Josh Aug 25 '17 at 04:55