Sign in to follow this  

System-wide mouse and keyboard hooking

This topic is 4084 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Since I can't prove to you guys that I'm not writing a trojan, I'll just hope no one assumes that by default. In any case, I need to set up a system-wide hook (keyboard and mouse) for the following purposes: * key combination detection in a background process while in fullscreen mode (much like Winamp Agent) * mouse position tracking (yes, I could use polling, but I also want to trap certain events) I've set up the following simple framework: Main application:
	HMODULE lib = LoadLibraryA("dllname.dll");
	//FUNCSTOP funcstop = (FUNCSTOP)GetProcAddress(lib, "stop");
	FUNCSETUP funcsetup = (FUNCSETUP)GetProcAddress(lib, "setup");

        //registers the callback proc that the DLL should call when it traps an event and initializes the hooks
	funcsetup(hookproc);


The DLL:
extern "C" DLL_API void setup(FUNC_CALLBACK cb)
{
        //global; allow only one instance
	if(isInitialized)
		return;

	isInitialized = true;

	callback = cb;

	mHook = SetWindowsHookEx(WH_MOUSE,(HOOKPROC)mook, module, 0);
	kHook = SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)kook, module, 0);
}

LRESULT CALLBACK kook(int iCode, WPARAM wParam, LPARAM lParam)
{	
	callback(1, iCode, wParam, lParam);

	return CallNextHookEx(NULL, iCode, wParam, lParam);
}

LRESULT CALLBACK mook(int iCode, WPARAM wParam, LPARAM lParam)
{
	callback(0, iCode, wParam, lParam);

	return CallNextHookEx(NULL, iCode, wParam, lParam);
}


The above example works only for the keyboard hook and only after I run the application. Which means: when I run the exe, keyboard evens are trapped and continue to be trapped if I give focus to the any of the application's windows. However, as soon as I switch to any other window, keyboard events seize to be trapped and aren't trapped even if I switch back to the application. Mouse events aren't trapped at all. NOTE: module used in setup() is obtained in DLLMain, so it's not NULL. In general, this seems like a very simple solution for a problem (hooking) and I can't find anything remotely complex about it, but for some reason it just doesn't seem to be working. Can anyone spot a problem with the above code? Bonus question: This article describes the implementation of a system-wide keyboard hook (and as far as I can tell, my code is pretty similar to it, only considerably more lightweight). When I download the demo or the source, though, AVG reports KeyHook.dll as a trojan (PSW.Agent.AKZ). The weird thing is that I've shut AVG down (also the background services) and now KeyHook.dll doesn't simply have write or read access (I can't copy it in Explorer - even though I can delete it - and running the sample application will crash - as best I can tell, because of KeyHook.dll being blocked). Does anyone else get that? I'm using XP + SP2.

Share this post


Link to post
Share on other sites
Sounds more like you're trying to write a 'bot, rather than a trojan. But still, there are legitimate reasons for writing a global hook.

For example, here is a program I wrote that installs a global mouse hook that translates middle-button clicks into left-button double-clicks.

It includes source, so should get you started.

Share this post


Link to post
Share on other sites
Thanks, Dean.

The odd thing is that WH_MOUSE and WH_KEYBOARD don't seem to be working as I would expect (maybe my expectations are wrong). WH_MOUSE_LL, however, is working just okay (haven't tested WH_KEYBOARD_LL yet).

As for assumptions, I'm writing this application for personal use at this time - I have a problem with certain applications running at some x refresh rate and locking up the system by switching the monitor to some weird state that doesn't cause it to switch off, but disables its OSD and probably causes it to go into some self-destructive mode. If the application, then, doesn't allow minimizing (multitasking), then the only way to restore the system is to either terminate the process or restart the computer. I'm sure you can figure out the application of my little utility by now.

I'm using mouse hooking for interactive feedback on window properties and possibly simple macroing (such as "run application, press button 1, press button 2, save, exit"), although I think the latter isn't really that useful.

PS - I thought global hooks could only reside in a DLL...

Share this post


Link to post
Share on other sites

This topic is 4084 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this