Archived

This topic is now archived and is closed to further replies.

malpass

[C# Mainly] Tracking Events when window isnt focused

Recommended Posts

How can I track events like mouse clicks and keyboard key presses when my window isnt the active control. What I need to do is on my program have a button that starts a process in which it should hide its window, taking it out of focus, then track events and store them until they press F12, then reshow the window and display the events that occured. However, when my window isnt the active window it doesn''t recieve any messages in the WndProc() at all, so how can I get the events that happen when its not focused. I''v tryed using NativeWindow and assigning the current active windows Handle to it, in effect recieving that windows messages, but it doesn''t work, it won''t send any messages, its like it doesn''t let you access another windows messages. Is there maybe just a way to access EVERY event that is passed into the system, so I can simple get the events from there, well, there must be because Spy++ is an example of a prog that does that.

Share this post


Link to post
Share on other sites
When searching for message pumps, it comes up with the addMessageFilter functions, which just basically allow you to check and modify the messages currently being recieved before they reach WndProc, but it doesnt actually add any messages, it still just gets them only if th window is active.

The window hooks are to do with the NativeWindow, that i''v already tryed and it doesn''t work.

Can anyone help me get past this?

Share this post


Link to post
Share on other sites
I''ve found something in C++

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/WinUI/WindowsUserInterface/Windowing/Hooks/HookReference/HookFunctions/SetWindowsHookEx.asp

Its all in that section to the right of the page, but I can''t understand how i''m supposed to put all that stuff into a working example, let alone converted to a working C# example.

Can someone help me here

Share this post


Link to post
Share on other sites
quote:
Original post by malpass
I''ve found something in C++

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/WinUI/WindowsUserInterface/Windowing/Hooks/HookReference/HookFunctions/SetWindowsHookEx.asp

Its all in that section to the right of the page, but I can''t understand how i''m supposed to put all that stuff into a working example, let alone converted to a working C# example.

Can someone help me here


Check out this page. It gives an example of how to set a keyboard hook in C#, as well as information about solving one of the first problems you will encounter---that is .NET garbage collecting your delegate but Windows continuing to use your function.

http://www.dotnetinterop.com/faq/?q=DelegateGC

Setting a mouse hook will be similar.

--TheMuuj

Share this post


Link to post
Share on other sites
thanks, thats just what I was after.

After using it with the mouse light flag it works fine but I cant find the Y value and I think the X is a bit wrong because it returns -2 and like 1100 of a 1024 res, things like that. And also if I try to use the full functions like WH_MOUSE it crashes, i think I have to process the Hook Procedure a different way for each event, but i tryed just a blank event on them and they still crashes.

I''ve searched msdn but theres no examples of uses of the WH_XXX''s window procedures or how to use these. Is there any tutorials anywhere or can you tell me what I have to do differently for each type of WH_ please. Oh and how to get the X and Y from the WH_MOUSE_LL window procedure

Share this post


Link to post
Share on other sites