Gabriel McAdams
Gabriel McAdams

Reputation: 58293

Global low level keyboard hook - Race Condition

I have written a c# application that contains a WinAPI low level keyboard hook that I use to prevent all but alphanumeric keys.

The problem is - other programs (those that start at login) also have keyboard hooks (to start applications such as calculator, browser, etc). These programs have already opened these applications before my hook callback has been reached, therefore, there is no way for me to stop the action.

Is there any way to ensure that my hook is called before any others?


EDIT

To clear up confusion - My application is a single executable that has no installer. It is run on demand.

I need to be able to take over other hooks - or simply force my hook callback to be called first - Even though others have already called the SetWindowsHookEx method.

Upvotes: 3

Views: 734

Answers (2)

GalacticJello
GalacticJello

Reputation: 11455

While it does not answer how to do it in programatic way, there may be an acceptable solution to your specific problem if we come at it from a different angle...

What about going to a second-hand store, buying a cheap USB keyboard, and then snipping the connections to the keys you don't want your son to use? You could then connect the USB to your existing keyboard's USB port, and just push your keyboard out of the way when your son is playing.

When he is done, disconnect your son's keyboard and move yours back into place.

Otherwise, I think you may need to install WH_DEBUG hook to get at the keyboard message before it gets passed to any other hooks.

DebugProc function

The system calls this function before calling the hook procedures associated with any type of hook. The system passes information about the hook to be called to the DebugProc hook procedure, which examines the information and determines whether to allow the hook to be called.

The DebugProc will get passed the type of hook being called in wParam (WH_KEYBOARD_LL in your case), and a DEBUGHOOKINFO structure in lParam that contains the actual WH_KEYBOARD_LL hook info that would be passed to the hook chain.

Upvotes: 1

Rikon
Rikon

Reputation: 2706

can't you use your installer to edit the

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run]

registry value? I've not ever explicitly tried to do exactly what you're trying to do... I'm not sure if you can reorder these key values from the installer, but this should catch it early enough...

Or one of these: [HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run] [HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnce] [HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunServices] [HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunServicesOnce] [HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Winlogon\Userinit]

Upvotes: 0

Related Questions