   Possible to use hooks to log external button presses?
  Hi all,I work for a company that deals with electricity meter readings and I''m trying to be helpful (I''m not being paid for this!) by writing a program to log the number of reads successfully dealt with.  I understand that to do this, I think I need to be dealing with hooking messages.  I have found lots of guidance on how to hook mouse and keyboard messages, but what I''m looking for is an explanation of how to track when a particular button is pressed in an external application.  I''m pretty sure this is possible, as I have seen a program for Everquest that enables you to automate your gameplay, to do so, it runs in the background, and requires the user to press a hotkey before performing the required action in game, at which point, it would basically record your action for future use.  Now I''m wondering if I could write a program where the user hits a hotkey before pressing the button to be logged (so the program can fix on which button to be logging), and the program would record the number of instances in that day that the button is pressed, it could then create statistics, etc from this data.  Does anyone have any pointers as to what I should be lookin at to achieve this?  I don''t require a full walk-through, but just any advice or tutorials or anything that anyone could point me to would be much appreciated!!Cheers,Steve AKA Mephs 
  If you mean button as in on a keyboard, then it is GetAsynKeyState() or something similiar you are looking for.If you mean button as in a windowsgdi control, that can be done too, think i saw a tutorial on codeproject on it. ;  are you asking if you can intercept the messages sent by input devices (keyboard or mouse) when your application is not on focus?I think DirectInput can do this. It bypasses Windows messages, and deals directly with the input devices.Take a look at DISCL_BACKGROUND [edited by - alnite on February 2, 2004 4:48:27 PM];  If I understand you correctly, you want to intercept a button press in an external application and log a count of how many times the button is pressed. You''ll probably need to craft yourself a hook dll and a launcher program to install the hook dll. The relevant Win32 api is SetWindowsHookEx, probably using WH_GETMESSAGE - which means it would also be helpful to look up GetMsgProc. You probably could also set WH_CALLWNDPROC or WH_CALLWNDPROCRET hooks instead. Here''s some C sample code demonstrating WH_GETMESSAGE hook: MsgHook.zip. I wrote it to work with lcc-win32, so I can''t vouch for how it works with other compilers. The sample hooks WM_PAINT, you''ll need to modify it to hook WM_COMMAND and then compare the low order word of the wParam from the intercepted MSG structure. This part of the wParam is the resource identifier of the button. To determine the identifier of the button you''re targeting you''ll have to use a utility like PE Explorer to extract the resources from the target application. Once you have the resource files, you''ll have to locate the button you''re interested in to determine it''s ID. All this is presuming that the application that you''re targeting is fairly standard and the button that you''re interested in resides in a dialog. If the button was created as a child window of the main program frame, you might have to perform some experiments logging all the WM_COMMAND messages, then clicking the button and seeing what identifier appears. This reminds me, the sample code installs a global hook - it hooks every top level window - and that introduces a lot more system lag than you really need. Given that you know which program you want to target, there are ways to focus the hook onto the specific program - however, I don''t recall them off the top of my head.  All that said, be forewarned that hooking a window is not one of the easiest things to do - especially in straight C. ;  Thanks for all the responses!!Lessbread, I think you are closest to what I''ve been looking for, but I did manage to find a codeproject tutorial, describing how to launch an external application and set a hook.  It dealt with launching the display control panel window and selecting the "use large icons" checkbox.  This was perfect for my needs, basically it obtains the window handle and enumerates the child windows/controls, searching for a match with the control text, this then enables the program to send events to the control.  I just need to take it one step further and grab the identifier of the control, and then read messages, so I think if I combine it with the codeproject tutorial on hooking events, I should be able to achieve the monitoring that I''ve been looking to!!  Anyways, thanks for the source and explanation, I think it will come in handy in clearing up any as yet unsolved problems.Thanks very much!!Steve AKA MephsPS I''m all for sharing information, so if anyone else would like the link to the articles I used and/or any further info on the topic, just email me (I''m at work and don''t have the link handy right now) 