Search code examples
c#winapikeyboard-hookmousekeyhook

How to ask keyboard hook to process messages AFTER the parent window


The title says it all. How can I ask my keyboard hook to tackle incoming keyboard message AFTER the parent window has processed them? Reading the docs I found there is a value named WH_CALLWNDPROCRET that does exactly this, but I need to specify WH_KEYBOARD there (to make it a keyboard hook of course), and the value doesn't appear to be a bit flag, so I can't combine both.

Background: I'm writing an add-in for Word 2013, which needs to monitor certain keys and take appropriate action only after Word has finished processing those keys. I'm using globalmousekeyhook project for hooking.


Solution

  • The library you are referring to is subscribing to following four hooks:

    • WH_KEYBOARD_LL
    • WH_KEYBOARD
    • WH_MOUSE_LL
    • WH_MOUSE

    When you subscribe to a hook you give a system a callback to your code, which will be executed according to the rules which differ from hook type to hook type. Also the information the callback will deliver to you must be interpreted differently. These 4 deliver information about mouse positions, key strokes etc.

    There are many different types of hooks you can subscribe to. See: Hook Overview

    The WH_CALLWNDPROCRET yo are referring to is one of them. It has different callback invocation behavior and delivers you all messages sent to window. These may include theoretically any of hundreds of possible messages, not only keyboard and mouse messages.

    To answer your question the library globalmousekeyhook can not subscribe to any other hooks than those 4 mentioned above.

    Good news is that you can probably reuse code form the library to implement your own subscription.

    • You can reuse code to install the hook.
    • The signature of your callback will of course be different.
    • Then you will get all messages.
    • Filter out only those messages you are interested in e.g. WM_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN, WM_SYSKEYUP.
    • Interpret the data delivered along with messages. Also here you can reuse some code from the library.

    Conclusion No, the library can not do what you are looking for. Yes, you can probably achieve that by reusing code from that library.