觅风论坛

标题: 拦截键鼠输入实现转发阻挡修改充当中间件C++连招实例源码免费分享 [打印本页]

作者: 随缘唯美    时间: 6 小时前
标题: 拦截键鼠输入实现转发阻挡修改充当中间件C++连招实例源码免费分享
fuzhu与无障碍功能:为有特殊需求的用户定制输入方式。


工业与专业应用:用于航空训练模拟器、监控与数据采集系统(SCADA)、超市收银系统定制等。


自动化与效率工具:用于家庭影院自动化、构建系统级的Emacs编辑模式等。

游戏与安全研究:用于游戏控制自定义、输入模式识别(如博士论文中的击键模式识别研究)等。

其他创造性用途:项目作者表示,它已被用于许多他最初未曾想到的领域。


简单来说,这个工具像一个“中间人”,可以捕获键盘按键和鼠标点击等输入信号,然后让程序决定是让信号原样通过、进行修改,还是完全阻止。

下面是实例:其他可自由发挥!
// combo_key.cpp
// 编译命令: cl combo_key.cpp interception.lib

#include <iostream>
#include <windows.h>
#include <thread>
#include <chrono>
#include "interception.h"

using namespace std;

// 定义键盘按键的扫描码(Scan Code)
#define SCANCODE_Q 0x10
#define SCANCODE_W 0x11
#define SCANCODE_E 0x12
#define SCANCODE_R 0x13
#define SCANCODE_ESC 0x01

// 定义鼠标侧键(不同鼠标可能不同,X1通常是0x05,X2是0x06)
#define MOUSE_BUTTON_X1 0x05  // 侧键1(前进键)
#define MOUSE_BUTTON_X2 0x06  // 侧键2(后退键)

class ComboKeyManager {
private:
    InterceptionContext context;
    InterceptionDevice keyboard_device;
    InterceptionDevice mouse_device;
    bool running;
   
    // 发送单个键盘按键事件
    void send_key(InterceptionDevice device, unsigned short scan_code, bool key_down) {
        InterceptionKeyStroke stroke;
        stroke.code = scan_code;
        stroke.state = key_down ? INTERCEPTION_KEY_DOWN : INTERCEPTION_KEY_UP;
        
        interception_send(context, device, (InterceptionStroke*)&stroke, 1);
    }
   
    // 执行连招:依次按下 Q->W->E->R
    void execute_combo() {
        // 获取键盘设备(如果尚未获取)
        if (keyboard_device == 0) {
            keyboard_device = interception_get_keyboard(context, 0);
            if (keyboard_device == 0) {
                cerr << "未能找到键盘设备!" << endl;
                return;
            }
        }
        
        cout << "执行连招: Q->W->E->R" << endl;
        
        // 按键序列和延迟(单位:毫秒)
        unsigned short combo_keys[] = {SCANCODE_Q, SCANCODE_W, SCANCODE_E, SCANCODE_R};
        int delays[] = {50, 30, 40, 100};  // 按键之间的延迟
        
        // 依次按下并释放每个键
        for (int i = 0; i < 4; i++) {
            // 按下键
            send_key(keyboard_device, combo_keys, true);
            this_thread::sleep_for(chrono::milliseconds(10));  // 短暂延迟
            
            // 释放键
            send_key(keyboard_device, combo_keys, false);
            
            // 等待指定延迟(最后一个键后不等待)
            if (i < 3) {
                this_thread::sleep_for(chrono::milliseconds(delays));
            }
        }
        
        cout << "连招执行完毕!" << endl;
    }

public:
    ComboKeyManager() : context(nullptr), keyboard_device(0), mouse_device(0), running(false) {}
   
    ~ComboKeyManager() {
        stop();
    }
   
    // 初始化 Interception
    bool initialize() {
        // 创建 Interception 上下文
        context = interception_create_context();
        if (!context) {
            cerr << "无法创建 Interception 上下文!" << endl;
            return false;
        }
        
        // 设置要拦截的设备过滤器
        interception_set_filter(context, interception_is_keyboard,
                              INTERCEPTION_FILTER_KEY_DOWN | INTERCEPTION_FILTER_KEY_UP);
        interception_set_filter(context, interception_is_mouse,
                              INTERCEPTION_FILTER_MOUSE_BUTTONS);
        
        cout << "Interception 初始化成功!" << endl;
        cout << "程序说明:" << endl;
        cout << "1. 按下鼠标侧键1(前进键)执行 Q->W->E->R 连招" << endl;
        cout << "2. 按下 ESC 键退出程序" << endl;
        cout << "3. 请确保以管理员权限运行本程序" << endl;
        cout << "正在监听输入..." << endl;
        
        return true;
    }
   
    // 主运行循环
    void run() {
        if (!context) {
            cerr << "请先调用 initialize() 初始化!" << endl;
            return;
        }
        
        running = true;
        InterceptionStroke stroke;
        
        while (running) {
            // 等待输入事件(最多等待10毫秒,以便可以检查运行状态)
            InterceptionDevice device = interception_wait_with_timeout(context, 10);
            
            if (device > 0 && interception_receive(context, device, &stroke, 1) > 0) {
                // 判断设备类型
                if (interception_is_keyboard(device)) {
                    InterceptionKeyStroke& kstroke = *(InterceptionKeyStroke*)&stroke;
                    
                    // 检查是否为 ESC 键(退出程序)
                    if (kstroke.code == SCANCODE_ESC &&
                        (kstroke.state == INTERCEPTION_KEY_DOWN ||
                         kstroke.state == INTERCEPTION_KEY_UP)) {
                        cout << "检测到 ESC 键,退出程序..." << endl;
                        running = false;
                        // 转发 ESC 键事件
                        interception_send(context, device, &stroke, 1);
                        break;
                    }
                    
                    // 转发所有键盘事件(除了我们要特殊处理的)
                    interception_send(context, device, &stroke, 1);
                }
                else if (interception_is_mouse(device)) {
                    InterceptionMouseStroke& mstroke = *(InterceptionMouseStroke*)&stroke;
                    
                    // 检查是否为鼠标侧键按下
                    if ((mstroke.state & INTERCEPTION_MOUSE_BUTTON_1_DOWN) &&
                        (mstroke.flags & MOUSE_BUTTON_X1)) {
                        cout << "检测到鼠标侧键1按下" << endl;
                        // 不转发这个侧键事件(即阻止它)
                        execute_combo();
                    }
                    else if ((mstroke.state & INTERCEPTION_MOUSE_BUTTON_1_DOWN) &&
                             (mstroke.flags & MOUSE_BUTTON_X2)) {
                        cout << "检测到鼠标侧键2按下" << endl;
                        // 不转发这个侧键事件
                        execute_combo();
                    }
                    else {
                        // 转发其他所有鼠标事件
                        interception_send(context, device, &stroke, 1);
                    }
                }
                else {
                    // 转发其他设备事件
                    interception_send(context, device, &stroke, 1);
                }
            }
        }
    }
   
    // 停止运行
    void stop() {
        running = false;
        if (context) {
            interception_destroy_context(context);
            context = nullptr;
        }
        cout << "程序已停止。" << endl;
    }
};

// 主函数
int main() {
    cout << "=== 一键连招程序 ===" << endl;
    cout << "注意:请以管理员身份运行此程序!" << endl;
   
    ComboKeyManager manager;
   
    if (!manager.initialize()) {
        cerr << "初始化失败!请确保:" << endl;
        cerr << "1. 以管理员身份运行" << endl;
        cerr << "2. 已安装 Interception 驱动程序" << endl;
        cerr << "3. interception.h 和 interception.lib 在正确位置" << endl;
        system("pause");
        return 1;
    }
   
    // 设置控制台Ctrl+C处理
    SetConsoleCtrlHandler([](DWORD event) -> BOOL {
        if (event == CTRL_C_EVENT) {
            cout << "\n接收到 Ctrl+C,退出程序..." << endl;
            return TRUE;
        }
        return FALSE;
    }, TRUE);
   
    cout << "程序已启动。按 ESC 键退出。" << endl;
   
    try {
        manager.run();
    }
    catch (const exception& e) {
        cerr << "程序异常: " << e.what() << endl;
    }
   
    system("pause");
    return 0;
}



作者: 唉唉唉    时间: 5 小时前
这个不错,谢谢共享
作者: 霸王喝粥    时间: 5 小时前
不错哦  喜欢 嘿嘿
作者: zf123456    时间: 5 小时前
前来围观,LZ好样的!
作者: 鸟文是帅逼    时间: 5 小时前
赞一个!
作者: jiuse    时间: 半小时前
让 江小白 来看看帖子里藏了啥好东西~~~




欢迎光临 觅风论坛 (https://www.eyyba.com/) Powered by Discuz! X3.4