// 【完美的 小写的】
#include <conio.h> // 用于_kbhit和_getch
#include <iostream>
#include <fstream>
#include <windows.h> // 需要包含windows.h以使用_kbhit(), _getch(), GetAsyncKeyState(), Sleep()
#include <cctype> // 包含这个头文件来使用tolower()函数
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) { // 如果是ESC键,退出循环
break;
}
// 使用tolower将字符转换为小写
char lowerCh = std::tolower(static_cast<unsigned char>(ch));
if (lowerCh) { // 如果映射成功(非零字符)
outfile << lowerCh;
std::cout << lowerCh;
}
}
else {
// 检查F1到F12是否被按下
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);
}
}
// 可以添加一个小延迟来减少CPU占用
Sleep(10);
}
}
outfile.close(); // 关闭文件
}
int main() {
KeyMonitor();
return 0;
}```
键盘记录+文件写入
最新推荐文章于 2024-05-22 00:03:53 发布