SetWindowsHookEx lost when switching appl

i've made a test with SetWindowsHookEx to capture keyboard messages to MY OWN APPLICATION. it works, except the fact that the hook doesn't hook anything else when i use the keyboard in another application. when i switch back to my own application, the hook doesn't hook my keyboard inputs anymore.

why ? how to solve the problem ?

here is how i create the hook :

HHOOK hookdata;
hookdata = SetWindowsHookEx( WH_KEYBOARD, KeyboardProc, hInstance, 0 );

and here is my callback function :

LRESULT CALLBACK KeyboardProc ( int code, WPARAM w, LPARAM l )
{
MessageBox( g_hWnd, "KEYBOARD", "HOOKED", MB_OK );
return 0;
}

Comments

  • Well, you need a system-wide hook to be able to make your hook work with processes that werent created by you. For this, you need to put your hook proc in a DLL and maybe use a shared data segment if you need to pass data around.

    Jeffery Richter's book Advanced Windows covers all that and more, or you can get this stupid little hook I once wrote that adds an "On Top" item to every window's system menu. It's written around WH_CBT but you can see how its done and modify it (or add to it) to work with WH_KEYBOARD or WH_KEYBOARD_LL.

    The code is here, VC++ 5 and later (if it doesnt work, copy/paste into your browser address bar):
    http://geocities.com/al_ani/kst.zip

    : i've made a test with SetWindowsHookEx to capture keyboard messages to MY OWN APPLICATION. it works, except the fact that the hook doesn't hook anything else when i use the keyboard in another application. when i switch back to my own application, the hook doesn't hook my keyboard inputs anymore.
    :
    : why ? how to solve the problem ?
    :
    : here is how i create the hook :
    :
    : HHOOK hookdata;
    : hookdata = SetWindowsHookEx( WH_KEYBOARD, KeyboardProc, hInstance, 0 );
    :
    : and here is my callback function :
    :
    : LRESULT CALLBACK KeyboardProc ( int code, WPARAM w, LPARAM l )
    : {
    : MessageBox( g_hWnd, "KEYBOARD", "HOOKED", MB_OK );
    : return 0;
    : }
    :

  • i only wanted to hook my own application but it's ok now. the solution :

    hookdata = SetWindowsHookEx( WH_KEYBOARD, KeyboardProc, hInstance, GetCurrentThreadId() );
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories