Using global keyboard hook (WH_KEYBOARD_LL) in WPF / C#

You’re creating your callback delegate inline in the SetHook method call. That delegate will eventually get garbage collected, since you’re not keeping a reference to it anywhere. And once the delegate is garbage collected, you will not get any more callbacks.

To prevent that, you need to keep a reference to the delegate alive as long as the hook is in place (until you call UnhookWindowsHookEx).

Leave a Comment