键盘记录+文件写入

6 篇文章 0 订阅
5 篇文章 0 订阅
// 【完美的 小写的】
#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;
}```

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值