22

What is the difference between the working of two? For WH_KEYBOARD_LL, I read that it Installs a hook procedure that monitors low-level keyboard input events. What is meant by low-level keyboard events?

Pang
  • 9,564
  • 146
  • 81
  • 122
program-o-steve
  • 2,630
  • 15
  • 48
  • 67

1 Answers1

38

Meh, don't focus too much on the term, it doesn't clarify anything. There's a huge difference between the two. WH_KEYBOARD_LL installs a hook that requires the callback to be implemented in your own program. And you must pump a message loop so that Windows can make the callback whenever it is about to dispatch a keyboard message. Which makes it really easy to get going.

WH_KEYBOARD works very differently, it requires a DLL that can be safely injected into hooked processes. Which makes it notoriously difficult to get going, injecting DLLs without affecting a process isn't easy. Particularly on a 64-bit operating system. Nor is taking care of the inter-process communication you might need if some other process needs to know about the keystroke. Like a key logger.

The advantage of WH_KEYBOARD is that it has access to the keyboard state. Which is a per-process property in Windows. State like the active keyboard layout and the state of the modifier and dead keys matter a great deal when you want to use the hook to translate virtual keys to typing keys yourself. You can't reliably call ToUnicodeEx() from an external process.

You can't swing a cat without running into example code, google will give you plenty. So be sure to use something known-to-work if you have never written such a hook before, it will avoid a lot of grief.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • 1
    _`"...installs a hook that requires the callback to be implemented..."`_. what do you mean by _callback_ ? – program-o-steve May 23 '12 at 11:44
  • 2
    Pretty sure that was already explained to you in a previous question. If it is still that mysterious then do consider leaving this project on the shelf until you've had time to learn more about basic winapi and programming in general. Getting hooks going is not trivial. – Hans Passant May 23 '12 at 11:48
  • let me verify,_"it means calling the next method in the hook chain" ?_ – program-o-steve May 23 '12 at 12:00
  • 5
    No. The 2nd argument for SetWindowsHookEx() is a function pointer. To the callback function. The one that Windows calls when something interesting happened. Consult your favorite C language programming book about function pointers. – Hans Passant May 23 '12 at 12:05