Archived

This topic is now archived and is closed to further replies.

griffenjam

Keyboard hook help

Recommended Posts

This is kinda a repost from something in the lounge, ohh well, this is more detailed. I''m trying to write a low level keyboard hook and it''s not working. SetWindowsHookEx returns a valid hook, but when I set a breakpoint in my hook function it never breaks there. WTF?
#define _WIN32_WINNT 0x0400

#include
#include



__declspec(dllexport) LRESULT CALLBACK KeyboardCallBack(long nCode, long wParam, long lParam);

HHOOK hook;
bool bRun = true;

WINAPI WinMain(HINSTANCE hPrevInstance, HINSTANCE hThis, LPSTR lpCmdLIne, int nCmdShow)
{
   char sLastError[15];
   HMODULE mod_han = GetModuleHandle(NULL);

   hook = SetWindowsHookEx(WH_KEYBOARD_LL, (HOOKPROC) KeyboardCallBack, mod_han, NULL);

   if(hook == 0)
   {
      sprintf(sLastError, "%d", GetLastError());
      MessageBox(NULL, sLastError, "Error", MB_OK);
      return 0;
   }

   while(bRun == true)
      Sleep(5000);
     
   UnhookWindowsHookEx(hook);

   return 0;
}

__declspec(dllexport) LRESULT CALLBACK KeyboardCallBack(long nCode, long wParam, long lParam)
{
   MessageBox(NULL, "Hello World!", "Message", MB_OK);
 
   return CallNextHookEx(hook, nCode,wParam,lParam);
}
 

Share this post


Link to post
Share on other sites
okay, I almost have it working.
The only thing it can''t do that I want it to is change the data if a certain key is pressed. Is that possible with the WH_KEYBOARD_LL hook? I read that it isn''t possible with the WH_KEYBOARD hook.

Can I do this? How?

Share this post


Link to post
Share on other sites
The lParam of the hook contains a pointer to a KBDLLHOOKSTRUCT structure. The structure in turn contains the key scan code.
Now, if the scan code is of the desired key, just change it and pass the changed structure to the CallNextHookEx.
I''m not sure if you should set the Injected flag manually on the structure, but test it to see.

-Nik

Share this post


Link to post
Share on other sites
quote:
Original post by Nik02
Try logging the structure from the hook procedure, see if you''re getting expected codes.
NOTE: Don''t break in the procedure!!!


IT doesn''t work even when I run the program normally.

This is what my code looks like now.


__declspec(dllexport) LRESULT CALLBACK KeyboardCallBack(long nCode, WPARAM wParam, LPARAM lParam)
{
KBDLLHOOKSTRUCT hooked;
static int n=0;
unsigned char state[256];

if(nCode == HC_ACTION && wParam == WM_KEYDOWN)
{
hooked = *((KBDLLHOOKSTRUCT*)lParam);

if(hooked.vkCode == (int) ''w''-32)
{
n++;
// if(n>4)

{
hooked.vkCode = (int)''q''-32;
memcpy((void *)lParam, &hooked, sizeof(KBDLLHOOKSTRUCT));
hooked = *((KBDLLHOOKSTRUCT*)lParam);
n=0;
// return 1;

}
}
}

/* GetKeyboardState(state);
state[VK_CAPITAL] = 1;
SetKeyboardState(state);
*/

return CallNextHookEx(hook, nCode,wParam,lParam);
}

Share this post


Link to post
Share on other sites
My brains don't really work well this late... (1am, Finland)
I'll get back to you on this tomorrow, if i figure out a solution.

By the way, you can reuse the structure that windows allocated for the hook procedure, so copying it is not strictly necessary.

-Nik

EDIT:
Are you indeed expected to modify the memory that windows gives you, instead of copying it?

[edited by - Nik02 on October 22, 2003 6:05:36 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Nik02
My brains don''t really work well this late... (1am, Finland)
I''ll get back to you on this tomorrow, if i figure out a solution.

By the way, you can reuse the structure that windows allocated for the hook procedure, so copying it is not strictly necessary.

-Nik


So what da f* are you doing connected instead of bein at molly malones then? %)

Share this post


Link to post
Share on other sites