-1

I am trying to get when the left mouse button up is hapenning on a specific window. So I googled and find an interesting code snippet here.

That code snippet detects in which window the mouse click was done so i could modify a little to make it work. But the problem is that the hook using SetWindowsHookEx is not being set on a specific window, instead it listens for all mouse clicks in all windows (independently of the window) so i am interested in only set the hook in a specific window.

I have the window handle of the window in which i am interested in but now i need to know how to set the hook only in this window. How can i do this? I don't want to listen mouse events globally on all windows.

Thank you very much and sorry, i am completely new in using hooks.

Willy
  • 9,848
  • 22
  • 141
  • 284
  • Are you sure that's what you really want? Surely you discovered that a mouse hook interferes with normal usage of the left mouse button too much. You can limit to a specific window by calling GetForegroundWindow() in the hook callback but that still knackers normal mouse usage for that window. Boilerplate is to use RegisterHotkey() so a specific key for a specific window gets used, keyboard instead of mouse. – Hans Passant Feb 11 '23 at 11:07
  • The question you asked and the problem you are trying to solve are probably not the same thing. This is commonly called the [XY Problem](https://xyproblem.info). You'll need to explain what the problem is. If your immediate instinct is to repeat your question then you will need to take some time to understand the problem first. – IInspectable Feb 11 '23 at 12:11
  • @HansPassant yes, i have no other option. I am developing an VSTO Outlook Addin and it does not provide any mechanism for detecting mouse left button up on outlook main window. So i need to use some hook or Windows API method. I didn't know that setting a mouse hook interferes with it's normal usage, nobody says it in any forum. This is the first time i heard it. I only want to handle when left mouse button up is done in Outlook main window, no others. – Willy Feb 11 '23 at 13:19
  • @IInspectable i know what is the problem I have and what i want to solve. I have an Outlook VSTO Addin and i need to handle the left mouse button up in Outlook main window. Outlook API does not provide any mechanism for doing that so the only possible way is going through a hook or windows API method. But i want to listen left mouse button up events on Outlook main window only, no others. This is why i am.asking if i can set the hook only for a specific window, i don't want to detect that event in all windows. – Willy Feb 11 '23 at 13:25
  • That's not how you'd implement drag-and-drop functionality. – IInspectable Feb 11 '23 at 14:14
  • @HansPassant i am not interested in detecting keystrokes on a specific window nor i am not interested in any key event coming from the keyboard, instead i need that mouse event. – Willy Feb 11 '23 at 14:16
  • @IInspectable i don't want to detect drag-and-drop in any window, this is not my goal. Where am i saying in my post that i want to detect drag-and-drop? Nowhere. – Willy Feb 11 '23 at 14:21
  • I doubt you have understood the problem you are trying to solve just yet. Unless you take the time to understand what it is, and update the question once that has happened, there's little we can do to help, outside of stating that filtering a set on a given predicate is a solved problem. – IInspectable Feb 11 '23 at 14:29
  • Usually, nobody is interested in the *Mouse Button Up* on itself. You're probably trying to capture some sort of *action*, something that *happens*, or to pre-emptively act on something that supposedly will happen. Maybe `SetWinEventHook()` or UI Automation can help accomplish what you're actually after. Whatever that is, you didn't really say it – Jimi Feb 11 '23 at 14:52

1 Answers1

1

SetWindowsHookEx() hooks are installed either globally or per-thread. It has no concept of windows.

The code you linked to is using a WH_MOUSE_LL hook, which can be used only globally not per-thread, and as such its callback does not give you any information about which window receives each mouse event. It does, however, give you the screen coordinates of each mouse event. You can determine the current HWND located at those coordinates by using WindowFromPoint() and ChildWindowFromPoint/Ex().

On the other hand, an WH_MOUSE hook can be installed per-thread. You can get the thread ID of a given HWND by using GetWindowThreadProcessId(). This way, the hook callback will give you mouse events only for windows owned by that thread, and will also give you the HWND that receives each event, so you can ignore events for windows you are not interested in.

Note: if the HWND you are interested in monitoring belongs to another process than the one that is installing the hook, you will have to implement your hook callback in a DLL. Doing that is not required when using a WH_MOUSE_LL hook, or when hooking a specific thread that is owned by the same process that is installing the hook.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770