Where are the messages going to?

hi everybody

I've already successfully implemented a globally low level keyboard and mouse hook and it seems that this is all .net managed code could provide in global hooking. But after I got the message by the hook, where is this keyboard or mouse message going to the parameters and structures of hook event don't contain that information. any idea

thanks in advance




Answer this question

Where are the messages going to?

  • Alex Yakhnin - MSFT

    thanks Peter, but isn't it that when a message is posted to the message queue, the handle(hwnd) will be specified I mean in both SendMessage() and PostMessage(), the hwnd is in the parameter

  • ktto

    I'm writing program to redirect keys and hotkeys of a specific application. So after I get the keyboard messages I would like to know if the targeted application will receive them. If so, I can do something in the hook.

  • Robert A. Swirsky

    You don't know where it goes, you just know what the message is and that it will be going to one or more applications.

  • Chris Holt

    It could be going to more than one applications

  • mhadamji

    All messages go to a window, whether posted or sent.

    You don't get a window handle because there's really nothing you can do with it. WH_KEYBOARD_LL is an out-of-process hook, which is processed in its own process, not the process for which the message is intended. If you did have a window handle, you couldn't use SendMessage because you're on a different thread; and if you use PostMessage you can't pass any pointers to anything in your address space because you're in a different address space than the destination application.

    You can try to get the current foreground window (the window with focus) with GetForegroundWindow(). That should be the window for which the message is destined. Keep in mind, the message you're getting in your hook proc is received at a different time to when it was "created", so in the time between when the message was created and when it was sent to your hook proc the foreground window may have changed.

    What do you want to do with the window handle



  • zulu5255

    If you could post what methods you implemented from user32 I could attempt to help you.

  • Tim Hunter

    well, I hooked globally with WH_KEYBOARD_LL so I could receive all the keyboard messages system wide. When the message came and the event fired, I got only three parameters:

    int nCode indicate if you should pass the keyboard message directly to the next hook

    UIntPtr wParam identifier of the keyboard message. It could be WM_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN, WM_SYSKEYUP

    IntPtr lParam a pointer to KBDLLHOOKSTRUCT structure, but there is no handle in it indicating where is this keyboard message going to

    How can I know where this message goes



  • joeydj

    marffin wrote:
    It could be going to more than one applications
    It could be going to a particular application, as well as Windows and any other applications in the hook chain.

    The application with the active window is the likely destination of the message.

  • Where are the messages going to?