Hello!
Little unimportant info about me:
I think i can program in c# in a quite good way,but
I would like to make my programs useful also for people who hasn't got .NET framework installed.
So i started to learn c and to rewrite my c# programs in c.
I'm writing a program ,and i have some problem with the global hotkeys.
I've been searching the WWW for a solution for ~ 12 hours now ,and i still couldn't get it to work, so that's why i decided to post here.
I'd like to Initalize a low level keyboard hook with the usage of SetWindowsHookEx(...) function, react to a keypress with the help of LowLevelKeyboardProc(...) function, finally un-initalize it with the UnhookWindowsHookEx(...) function.
I read that it is possible without using a dll file since it's low level, and i would be glad to do that.
Here is my code:

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
LRESULT CALLBACK LowLevelKeyboardProc(
    int nCode,
    WPARAM wParam,
    LPARAM lParam
);
HHOOK s;
int main()
{
//Initalize the hook
    s = SetWindowsHookEx(WH_KEYBOARD_LL,LowLevelKeyboardProc,NULL,GetCurrentThreadId());
    getchar();
// un-initalize the hook
    UnhookWindowsHookEx(s);
    return 0;
    }
LRESULT CALLBACK LowLevelKeyboardProc(/* this should run when a key is hit on the keyboard*/
    int nCode,
    WPARAM wParam,
    LPARAM lParam
)
{
   printf("...");// just to let me know that the LowLevelKeyboardProc's been used
    }

Please help me out if you can.
Andrew

Recommended Answers

All 4 Replies

Sorry Posted to wrong place :-(
I didn't really notice that here is a separate forum part for c programming.

I read that it is possible without using a dll file since it's low level

Yes it is.

So what's the problem?
a) From what I see this HOOK isn't global but limited to your calling thread. The procedure is hooked top-priority for your CURRENT thread ONLY.
Ditch the last parameter in SetWindowsHookEx() for NULL.
b) You will also need to typecast lParam to a KBDLLHOOKSTRUCT Struct.
c) Also LowLevelKeyboardProc should return a value.
d) You are not passing the MESSAGE but "eating" it in your Application's Hook. So if your current thread is that of your app, any key press you make will not be intercepted by your app at all but will be eaten in your hook, which by of course is top-priority.

This should clear things up.

Mis-click. Apologies.

Yes it is.

So what's the problem?
a) From what I see this HOOK isn't global but limited to your calling thread. The procedure is hooked top-priority for your CURRENT thread ONLY.
Ditch the last parameter in SetWindowsHookEx() for NULL.
b) You will also need to typecast lParam to a KBDLLHOOKSTRUCT Struct.
c) Also LowLevelKeyboardProc should return a value.
d) You are not passing the MESSAGE but "eating" it in your Application's Hook. So if your current thread is that of your app, any key press you make will not be intercepted by your app at all but will be eaten in your hook, which by of course is top-priority.

This should clear things up.

Thanks this worked. :))

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.