winapi - LowLevelKeyboardHook C -


this question has answer here:

i try write litte hook programm in c. programm don't work , don't know why.

#include <stdio.h> #include <stdlib.h> #include <windows.h>  lresult callback lowlevelkeyboardproc( int ncode, wparam wparam, lparam lparam ) {     if(ncode >= 0)     {         char key;         kbdllhookstruct *pkeyboard = (kbdllhookstruct *)lparam;         key = (char)pkeyboard->vkcode;         printf("%c\n",key);     }     return callnexthookex(null, ncode, wparam, lparam); }  int main(void) {     hinstance instance = loadlibrary("user32");     hhook hook = setwindowshookex( wh_keyboard_ll, lowlevelkeyboardproc, instance, 0);     getchar();     unhookwindowshookex(hook);     printf("ready");     return exit_success; } 

i think mistake somewhere @ setwindowshookex function. when start it, delays keyboardinputs seconds , don't call function lowlevelkeyboardproc.

what wrong in code?

thanks help

if read documentation, says:

this hook called in context of thread installed it. call made sending message thread installed hook. therefore, thread installed hook must have message loop.

...

... however, the wh_keyboard_ll hook not injected process. instead, context switches process installed hook , called in original context. context switches application generated event.

your code not have message loop receive hook's messages.

try more instead:

#include <stdio.h> #include <stdlib.h> #include <windows.h>  lresult callback lowlevelkeyboardproc( int ncode, wparam wparam, lparam lparam ) {     if (ncode == hc_action)     {         kbdllhookstruct *pkeyboard = (kbdllhookstruct *)lparam;         char key = (char) pkeyboard->vkcode;         printf("%c\n", key);         postquitmessage(0);     }     return callnexthookex(null, ncode, wparam, lparam); }  int main(void) {     hhook hook = setwindowshookex(wh_keyboard_ll, lowlevelkeyboardproc, null, 0);     if (!hook)     {         printf("error setting hook: %u", getlasterror());         return -1;     }      msg msg;     while (getmessage(&msg, null, 0, 0) > 0)     {         translatemessage(&msg);         dispatchmessage(&msg);     }     unhookwindowshookex(hook);      printf("ready");     return exit_success; } 

Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -