3

i want to write a global keyboard hook to disallow task switching.When i googled i found a whole lot of codes in c#,cpp (and delphi), but i need some basic concepts about hooking (would be the best if examples are in C).So, kindly suggest the resources,links that can help me understand the thing in C's perspective.

PS: I found one good working example(works on winXP and older versions),but when i tried compiling the code it gives me: enter image description here

And i tried searching the "IDC_" constants in all the headers(default ones that come with MinGW gcc installation and the ones provided by developer),but no luck...If any one can compile the code and make it run please help me.I have not uploaded the source itself here as there are a few header file dependencies and in that case i'd have to post all the code here.

winXP is the target environment but would be better if i get it to run Win7 also.

Community
  • 1
  • 1
buch11
  • 872
  • 3
  • 13
  • 29
  • 1
    For what platform do you intend to use this? WinXP? If so Winapi has a way to implement global keyboard hooks. – Lefteris Feb 05 '12 at 13:27
  • i forgot to mention that in question...yeah winXP is the target environment but would be better if i get it to run Win7 also. – buch11 Feb 05 '12 at 13:38
  • I see, well in general I needed to see if it is under Windows. Check if my answer below might help. – Lefteris Feb 05 '12 at 13:42
  • This is a good case of "I want to solve X, and I think the convoluted solution is Y. I will ask about Y." What you really should probably do is ask about X. Capturing keystrokes does not prevent task switching. – tenfour Feb 05 '12 at 13:57
  • @tenfour why? if i disable ALT and WIN keys will you still be able to switch tasks? – buch11 Feb 05 '12 at 14:07
  • Yes, there are lots of ways of "switching tasks". – tenfour Feb 05 '12 at 14:37
  • it would be an enhancement to my knowledge if you share a few(going a bit off topic) – buch11 Feb 06 '12 at 01:51
  • `CreateProcess()`, scheduled task launching, hitting Ctrl+alt+delete, media keys / browser keys, power button on my laptop, auto-updaters ... the list is endless. – tenfour Feb 06 '12 at 10:37

1 Answers1

11

I will go out on a limb here assuming you are on Windows and you want to capture global keystrokes. A way to do this is to use LowLevelHooks. Look at the following example:

Define this callback function somewhere in your code:

//The function that implements the key logging functionality
LRESULT CALLBACK LowLevelKeyboardProc( int nCode, WPARAM wParam, LPARAM lParam )
{
   char pressedKey;
   // Declare a pointer to the KBDLLHOOKSTRUCTdsad
   KBDLLHOOKSTRUCT *pKeyBoard = (KBDLLHOOKSTRUCT *)lParam;
   switch( wParam )
   {
       case WM_KEYUP: // When the key has been pressed and released
       {
          //get the key code
          pressedKey = (char)pKeyBoard->vkCode;
       }
       break;
       default:
           return CallNextHookEx( NULL, nCode, wParam, lParam );
       break;
   }

    //do something with the pressed key here
      ....

   //according to winapi all functions which implement a hook must return by calling next hook
   return CallNextHookEx( NULL, nCode, wParam, lParam);
}

And then somewhere inside your main function you would set the hook like so:

 //Retrieve the applications instance
 HINSTANCE instance = GetModuleHandle(NULL);
 //Set a global Windows Hook to capture keystrokes using the function declared above
 HHOOK test1 = SetWindowsHookEx( WH_KEYBOARD_LL, LowLevelKeyboardProc, instance,0);

More general information about hooks can be found here. You can also capture other global events with the same exact way only following the directions given in SetWindowsHooksEX documentation.

Lefteris
  • 3,196
  • 5
  • 31
  • 52