Skip to main content
GameDev.net gamedev.net
🔒 Locked

Keyboard hooks

Started by Schwammerl-Bob Jan 25, 2003 at 7:09 PM 3 replies 350+ views
Original Post
Schwammerl-Bob
Schwammerl-Bob
I want to catch key-events globally. The best way to do this might be SetWindowsHookEx(WH_KEYBOARD, &KeyboardProc, hInst, 0); but strangly the callback function is never executed. Anybody has an idea why ? thank you
niyaw
niyaw
does the call succeed?
Schwammerl-Bob
Schwammerl-Bob
yep.. I asked a friend and he said SetWindowsHookEx() must be called in a DLL to work properly. I will try that now...
thx for your help
niyaw
niyaw
the hook procedure must be in a dll. setwindowshookex doesn''t have to be called from a dll. although generally, you''d have the dll export a function calling setwindowshookex and saving the returned hook handle in a shared data segment for future use with callnexthookex.

you *are* calling callnexthookex, aren''t you?
Schwammerl-Bob
Schwammerl-Bob
still not working ...
this is the DLL-file


      
#pragma data_seg("Shared")
HHOOK g_hHook = NULL;
#pragma data_seg()
#pragma comment(linker, "/section:Shared,rws")


const char text_file[] = "D:\\hook.txt";


LRESULT CALLBACK KeyboardProc(int code, WPARAM wParam, LPARAM lParam )
{
FILE *file;

file = fopen(text_file, "a");
fprintf(file, "key pressed\n");
fclose(file);

return 0; // CallNextHookEx(g_hHook, code, wParam, lParam);

}


I removed the CallNextHookEx() temporarily only...

How do I get access to g_hHook in the main exe ?? g_hHook is in shared memory of the DLL ( at least it should be )

This is the app:


  
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR, int)
{
hook = SetWindowsHookEx(WH_KEYBOARD, &KeyboardProc, hInst, 0);

// simple message loop

return 0;
}


thx for your hlp



[edited by - Schwammerl-Bob on January 28, 2003 8:26:11 AM]

[edited by - Schwammerl-Bob on January 28, 2003 8:27:07 AM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.