3

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.

Gabriel McAdams
  • 56,921
  • 12
  • 61
  • 77
  • 2
    Just curiosity, Why do you need this? – L.B Aug 17 '12 at 18:56
  • @HassanBoutougha: why do you say spy? My post clearly states that I am preventing keystrokes. – Gabriel McAdams Aug 17 '12 at 19:32
  • 1
    @L.B: I wrote an application that allows my baby son to type on the keyboard without causing any problems. Except he keeps opening my browser and calculator. – Gabriel McAdams Aug 17 '12 at 19:33
  • you may also think of applications like [DeepFreeze](http://www.faronics.com/en-uk/enterprise/deep-freeze_en-uk-2/?) – L.B Aug 17 '12 at 20:21
  • ["All global hook functions must be in libraries."](http://msdn.microsoft.com/en-us/library/windows/desktop/ms644990%28v=vs.85%29.aspx) How comes your global hook is a solid executable? – Roman R. Aug 17 '12 at 21:49
  • @RomanR.: For simplicity. I also read that statement, and I started out that way, but I didn't want to have to pass around more than one file. – Gabriel McAdams Aug 17 '12 at 22:19
  • 1
    @RomanR. That does not apply to low level hooks. It's required for other global hooks, because they need to inject a dll into the target process. Low level hooks operate in the original process. – CodesInChaos Aug 21 '12 at 13:14

2 Answers2

1

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.

Community
  • 1
  • 1
GalacticJello
  • 11,235
  • 2
  • 25
  • 35
  • Thanks. The goal was to pull up a quick application, let him type, save the text (or email it), and close it - then get back to normal. Your suggestion would take so long, I would never do it. – Gabriel McAdams Aug 17 '12 at 22:22
  • As for the WH_DEBUG hook - There is one thing I am not clear on. What does `To prevent the system from calling the hook...` mean? Does it skip all hooks (and allow the key to be pressed)? or does it cancel the keystroke, like in the `WH_KEYBOARD_LL` hook? – Gabriel McAdams Aug 17 '12 at 22:24
  • You are, in effect, the debugger. You will get notified about before the hook, and have access to the data that will be passed to the hook, if you allow it. – GalacticJello Aug 21 '12 at 12:50
  • I believe low level hooks are not affected by debug hooks. "Debug hooks cannot track this type of low level keyboard hooks." – CodesInChaos Aug 21 '12 at 13:19
0

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]

Rikon
  • 2,688
  • 3
  • 22
  • 32
  • This wont work. My application is not installed. It is a single executable that is run on demand. I need something that forces my application to be first in the hook callbacks ONLY while its running. – Gabriel McAdams Aug 17 '12 at 19:37