吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 350|回复: 2
收起左侧

[经验求助] 寻求按C++键监听帮助

[复制链接]
kuyedexiaowo 发表于 2024-9-22 23:03
25吾爱币

#include <iostream>
#include <windows.h>
#include <thread>
#include <unordered_map>
#include <vector>
#include <string>
#include <functional>
#include <array>
#include <map>
using namespace std;

unordered_map<string, bool> flag_lock = {
    {"sun_lock", false},
    {"cd_lock", false}
};
string sun_lock = "sun_lock";
string cd_lock = "cd_lock";
unordered_map<int, bool> h_keys = {
    {0, false},
    {1, false}
};


// 注册热键
void register_hotkeys() {

    if (RegisterHotKey(NULL, 0, 0, VK_F1)) {
        cerr << "注册热键 F1 成功: "  << endl;
    }

    if (RegisterHotKey(NULL, 1, 0, VK_F2)) {
        cerr << "注册热键 F2 成功: "  << endl;
    }

}
// 热键处理函数
void process_hotkeys() {
    if (h_keys[0]) {
        flag_lock["sun_lock"] = !flag_lock["sun_lock"];
        cout << "阳光修改状态: " << flag_lock["sun_lock"] << endl;
        h_keys[0] = false;// 重置热键状态
    }
    cout << "当前 h_keys[0] 状态: " << h_keys[0] << endl;
    if (h_keys[1]) {
        flag_lock["cd_lock"] = !flag_lock["cd_lock"];
        cout << "植物无CD状态: " << flag_lock["cd_lock"] << endl;
        h_keys[1] = false; // 重置热键状态
    }
}
// 线程函数用于监听热键
void hotkey_listener() {
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0)) {
        if (msg.message == WM_HOTKEY) {
            if (msg.wParam < 2) {
                h_keys[msg.wParam] = true;
                cout << "捕获到热键: " << msg.wParam << endl; // 打印捕获的热键
                process_hotkeys();
            }
        }
        
    }
}

void sun_mod(HANDLE p_hwnd, long sun_num = 9999) {
    LONG data_1, data_2, data_3;
    SIZE_T bytesRead;
   
    // 直接修改内存数据
    ReadProcessMemory(p_hwnd, (LPCVOID)0x006A9EC0, &data_1, sizeof(data_1), &bytesRead);
    ReadProcessMemory(p_hwnd, (LPCVOID)(data_1 + 0x5560), &data_2, sizeof(data_2), &bytesRead);
    ReadProcessMemory(p_hwnd, (LPCVOID)(data_2 + 0x768), &data_3, sizeof(data_3), &bytesRead);
    WriteProcessMemory(p_hwnd, (LPVOID)data_3, &sun_num, sizeof(sun_num), &bytesRead);
    cout << "阳光修改成功" << data_3 << endl;
}

void cd_mod(HANDLE p_hwnd) {
    LONG data_1, data_2, data_3;
    SIZE_T bytesRead;

    if (!ReadProcessMemory(p_hwnd, (LPCVOID)0x006A9EC0, &data_1, sizeof(data_1), &bytesRead)) {
        cerr << "读取 data_1 失败,错误代码: " << GetLastError() << endl;
        return;
    }
    if (!ReadProcessMemory(p_hwnd, (LPCVOID)(data_1 + 0x768), &data_2, sizeof(data_2), &bytesRead)) {
        cerr << "读取 data_2 失败,错误代码: " << GetLastError() << endl;
        return;
    }
    if (!ReadProcessMemory(p_hwnd, (LPCVOID)(data_2 + 0x144), &data_3, sizeof(data_3), &bytesRead)) {
        cerr << "读取 data_3 失败,错误代码: " << GetLastError() << endl;
        return;
    }
    for (int i = 0; i < 10; i++) {
        LONG writeAddress = data_3 + 0x70 + i * 0x70;
        SHORT value = 1;
        if (!WriteProcessMemory(p_hwnd, (LPVOID)writeAddress, &value, sizeof(value), &bytesRead)) {
            cerr << "写入植物无CD失败,位置: " << writeAddress << ",错误代码: " << GetLastError() << endl;
        }
    }
}


BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam) {
    char windowTitle[256];
    GetWindowTextA(hwnd, windowTitle, sizeof(windowTitle));
    //cout << "窗口标题: " << windowTitle << endl;
    if (strcmp(windowTitle, "植物大战僵尸杂交版v2.5") == 1) {
        cout << "窗口标题: " << windowTitle << endl;
        *(HWND*)lParam = hwnd; // 找到窗口,保存句柄
        return FALSE; // 停止枚举
    }
    return TRUE; // 继续枚举
}
void main_loop() {
    while (true) {

        DWORD pid = 0;

        HWND hwnd = NULL;
        EnumWindows(EnumWindowsProc, (LPARAM)&hwnd);
        
        if (hwnd != NULL) {
            GetWindowThreadProcessId(hwnd, &pid);
            if (pid == 0) {
                cout << "无法获取 PID" << endl;
            }
        }
        else {
            cout << "窗口句柄无效" << endl;
        }
        
        HANDLE p_hwnd = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
        cout << "游戏pid: " << pid << endl;
        if (p_hwnd == NULL) {
            cerr << "无法打开进程,错误代码: " << GetLastError() << endl;
            Sleep(10000);
            continue;
        }
        else {
            cout << "成功打开进程"<< p_hwnd <<endl;
        }
        //F1 阳光修改 F2 植物无CD
        cout << "F1 阳光修改 F2 植物无CD"<< endl;
        process_hotkeys();
        // 根据状态调用相应的修改函数
        if (flag_lock["sun_lock"]) {
            cout << "准备调用 sun_mod" << endl; // 调试输出
            sun_mod(p_hwnd);
            
        }
        if (flag_lock["cd_lock"]) {
            cout << "准备调用 cd_mod" << endl; // 调试输出
            cd_mod(p_hwnd);
        }
        //
        CloseHandle(p_hwnd);
        

        Sleep(3000);
    }
}

int main() {
    register_hotkeys(); // 注册热键
    // 启动热键监听
    //tkey_listener();

    thread hotkey_thread(hotkey_listener);
    hotkey_thread.detach(); // 分离线程

    main_loop(); // 启动主循环

    return 0;
}[/mw_shl_code]

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

fortytwo 发表于 2024-9-23 15:28
再试试呢
[C++] 纯文本查看 复制代码
#include <iostream>
#include <windows.h>
#include <thread>
#include <unordered_map>
#include <vector>
#include <string>
#include <functional>
#include <array>
#include <map>
#include <mutex>

using namespace std;

// 共享数据和互斥锁
unordered_map<string, bool> flag_lock = {
    {"sun_lock", false},
    {"cd_lock", false}
};
string sun_lock_key = "sun_lock";
string cd_lock_key = "cd_lock";
unordered_map<int, bool> h_keys = {
    {0, false},
    {1, false}
};

// 互斥锁用于保护共享数据
mutex mtx;

// 注册热键
void register_hotkeys() {
    // 注册 F1 热键
    if (RegisterHotKey(NULL, 0, 0, VK_F1)) {
        cout << "注册热键 F1 成功." << endl;
    } else {
        cerr << "注册热键 F1 失败,错误代码: " << GetLastError() << endl;
    }

    // 注册 F2 热键
    if (RegisterHotKey(NULL, 1, 0, VK_F2)) {
        cout << "注册热键 F2 成功." << endl;
    } else {
        cerr << "注册热键 F2 失败,错误代码: " << GetLastError() << endl;
    }
}

// 热键处理函数
void process_hotkeys() {
    lock_guard<mutex> lock(mtx); // 锁定互斥锁以保护共享数据

    if (h_keys[0]) {
        flag_lock[sun_lock_key] = !flag_lock[sun_lock_key];
        cout << "阳光修改状态: " << flag_lock[sun_lock_key] << endl;
        h_keys[0] = false; // 重置热键状态
    }
    if (h_keys[1]) {
        flag_lock[cd_lock_key] = !flag_lock[cd_lock_key];
        cout << "植物无CD状态: " << flag_lock[cd_lock_key] << endl;
        h_keys[1] = false; // 重置热键状态
    }
}

// 热键处理线程函数
void hotkey_listener() {
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0)) {
        if (msg.message == WM_HOTKEY) {
            if (msg.wParam < 2) {
                {
                    lock_guard<mutex> lock(mtx); // 锁定互斥锁以保护共享数据
                    h_keys[msg.wParam] = true;
                }
                cout << "捕获到热键: " << msg.wParam << endl; // 打印捕获的热键
                process_hotkeys(); // 立即处理热键
            }
        }
    }
}

// 修改阳光数量的函数
void sun_mod(HANDLE p_hwnd, long sun_num = 9999) {
    LONG data_1, data_2, data_3;
    SIZE_T bytesRead;

    // 直接修改内存数据
    if (!ReadProcessMemory(p_hwnd, (LPCVOID)0x006A9EC0, &data_1, sizeof(data_1), &bytesRead)) {
        cerr << "读取 data_1 失败,错误代码: " << GetLastError() << endl;
        return;
    }
    if (!ReadProcessMemory(p_hwnd, (LPCVOID)(data_1 + 0x5560), &data_2, sizeof(data_2), &bytesRead)) {
        cerr << "读取 data_2 失败,错误代码: " << GetLastError() << endl;
        return;
    }
    if (!ReadProcessMemory(p_hwnd, (LPCVOID)(data_2 + 0x768), &data_3, sizeof(data_3), &bytesRead)) {
        cerr << "读取 data_3 失败,错误代码: " << GetLastError() << endl;
        return;
    }
    if (!WriteProcessMemory(p_hwnd, (LPVOID)data_3, &sun_num, sizeof(sun_num), &bytesRead)) {
        cerr << "写入阳光失败,错误代码: " << GetLastError() << endl;
        return;
    }
    cout << "阳光修改成功,地址: " << hex << data_3 << ", 新值: " << dec << sun_num << endl;
}

// 修改植物无CD的函数
void cd_mod(HANDLE p_hwnd) {
    LONG data_1, data_2, data_3;
    SIZE_T bytesRead;

    if (!ReadProcessMemory(p_hwnd, (LPCVOID)0x006A9EC0, &data_1, sizeof(data_1), &bytesRead)) {
        cerr << "读取 data_1 失败,错误代码: " << GetLastError() << endl;
        return;
    }
    if (!ReadProcessMemory(p_hwnd, (LPCVOID)(data_1 + 0x768), &data_2, sizeof(data_2), &bytesRead)) {
        cerr << "读取 data_2 失败,错误代码: " << GetLastError() << endl;
        return;
    }
    if (!ReadProcessMemory(p_hwnd, (LPCVOID)(data_2 + 0x144), &data_3, sizeof(data_3), &bytesRead)) {
        cerr << "读取 data_3 失败,错误代码: " << GetLastError() << endl;
        return;
    }

    for (int i = 0; i < 10; i++) {
        LONG writeAddress = data_3 + 0x70 + i * 0x70;
        SHORT value = 1;
        if (!WriteProcessMemory(p_hwnd, (LPVOID)writeAddress, &value, sizeof(value), &bytesRead)) {
            cerr << "写入植物无CD失败,位置: " << hex << writeAddress << ",错误代码: " << GetLastError() << endl;
        }
    }
    cout << "植物无CD修改成功." << endl;
}

// 枚举窗口的回调函数
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam) {
    char windowTitle[256];
    GetWindowTextA(hwnd, windowTitle, sizeof(windowTitle));
    // 检查窗口标题是否完全匹配
    if (strcmp(windowTitle, "植物大战僵尸杂交版v2.5") == 0) { // 修正比较条件
        cout << "找到目标窗口,标题: " << windowTitle << endl;
        *(HWND*)lParam = hwnd; // 找到窗口,保存句柄
        return FALSE; // 停止枚举
    }
    return TRUE; // 继续枚举
}

// 主循环函数
void main_loop() {
    while (true) {
        DWORD pid = 0;
        HWND hwnd = NULL;

        // 枚举窗口以找到目标窗口句柄
        EnumWindows(EnumWindowsProc, (LPARAM)&hwnd);

        if (hwnd != NULL) {
            GetWindowThreadProcessId(hwnd, &pid);
            if (pid == 0) {
                cerr << "无法获取 PID" << endl;
                Sleep(10000);
                continue;
            }
        } else {
            cerr << "未找到目标窗口." << endl;
            Sleep(10000);
            continue;
        }

        HANDLE p_hwnd = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
        cout << "游戏 PID: " << pid << endl;
        if (p_hwnd == NULL) {
            cerr << "无法打开进程,错误代码: " << GetLastError() << endl;
            Sleep(10000);
            continue;
        } else {
            cout << "成功打开进程句柄: " << p_hwnd << endl;
        }

        // 锁定互斥锁以访问共享数据
        {
            lock_guard<mutex> lock(mtx);
            // 根据状态调用相应的修改函数
            if (flag_lock[sun_lock_key]) {
                cout << "准备调用 sun_mod" << endl; // 调试输出
                sun_mod(p_hwnd);
            }
            if (flag_lock[cd_lock_key]) {
                cout << "准备调用 cd_mod" << endl; // 调试输出
                cd_mod(p_hwnd);
            }
        }

        CloseHandle(p_hwnd);
        Sleep(3000); // 每3秒检查一次
    }
}

int main() {
    // 注册热键
    register_hotkeys();

    // 启动热键监听线程
    thread hotkey_thread(hotkey_listener);
    hotkey_thread.detach(); // 分离线程

    // 启动主循环
    main_loop();

    // 注销热键(这部分代码实际上不会被执行,因为 main_loop 是无限循环)
    UnregisterHotKey(NULL, 0);
    UnregisterHotKey(NULL, 1);

    return 0;
}
 楼主| kuyedexiaowo 发表于 2024-9-24 01:08
fortytwo 发表于 2024-9-23 15:28
再试试呢
[mw_shl_code=cpp,true]#include
#include

抱歉 还是不行  并且这样找不到窗口
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-12-13 17:08

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表