#include <conio.h>
#include <iostream>
#include <fstream>
#include <windows.h>
#include <cctype>
void KeyMonitor() {
std::ofstream outfile("keylog.txt", std::ios::app);
if (!outfile.is_open()) {
std::cerr << "Error opening file for writing!" << std::endl;
return;
}
std::cout << "Press any character key or F1 to F12 to log them. Press ESC to exit." << std::endl;
while (true) {
if (_kbhit()) {
char ch = _getch();
if (ch == 27) {
break;
}
char lowerCh = std::tolower(static_cast<unsigned char>(ch));
if (lowerCh) {
outfile << lowerCh;
std::cout << lowerCh;
}
}
else {
for (int i = VK_F1; i <= VK_F12; ++i) {
if (GetAsyncKeyState(i) & 0x8000) {
outfile << "Pressed: F" << (i - VK_F1 + 1);
std::cout << "Logged: F" << (i - VK_F1 + 1);
}
}
Sleep(10);
}
}
outfile.close();
}
int main() {
KeyMonitor();
return 0;
}```