QT with Interception 驱动模拟键鼠
0. 安装
注意 官页这句话
First, you must download and extract the Interception assets and then run
install-interceptionThis tool must be run from an administrator command line (you must run cmd as administrator). Just run it without arguments to receive instructions.
$ install-interception.exe /install
Interception command line installation tool
Copyright (C) 2008-2018 Francisco Lopes da Silva
Interception successfully installed. You must reboot for it to take effect.
1. 引入头文件和lib
cmakelist.txt
# 设置头文件目录 相当于指定gcc的-I参数
include_directories(
library/
)
# 设置链接库搜索目录 相当于gcc的-L参数
link_directories(
library/x64/
)
target_link_libraries(Interception Qt${QT_VERSION_MAJOR}::Core
"interception" # 链接
)3. 测试
//The following sample shows how to intercept the x key and turn it into y: 拦截 x 键转为 y 键
enum ScanCode
{
SCANCODE_X = 0x2D,//X 键码
SCANCODE_Y = 0x15,
SCANCODE_ESC = 0x01//ESC 键码
};
void example1(){
using namespace std;
InterceptionContext context;
InterceptionDevice device;
InterceptionKeyStroke stroke;
//raise_process_priority();
context = interception_create_context();
interception_set_filter(context, interception_is_keyboard, INTERCEPTION_FILTER_KEY_DOWN | INTERCEPTION_FILTER_KEY_UP);
while(interception_receive(context, device = interception_wait(context), (InterceptionStroke *)&stroke, 1) > 0)
{
if(stroke.code == SCANCODE_X) stroke.code = SCANCODE_Y;
interception_send(context, device, (InterceptionStroke *)&stroke, 1);
if(stroke.code == SCANCODE_ESC) break;
}
interception_destroy_context(context);
}
void muni(){
//初始化上下文
InterceptionContext context = interception_create_context();
// 鼠标移动到屏幕中间
InterceptionMouseStroke mouseStroke[3];
mouseStroke[0].flags = INTERCEPTION_MOUSE_MOVE_ABSOLUTE;
mouseStroke[0].x = 65535 / 2; // 坐标取值范围是0-65535
mouseStroke[0].y = 65535 / 2;
// 点击鼠标右键
mouseStroke[1].state = INTERCEPTION_MOUSE_RIGHT_BUTTON_DOWN;
mouseStroke[2].state = INTERCEPTION_MOUSE_RIGHT_BUTTON_UP;
//发送
interception_send(context, INTERCEPTION_MOUSE(0), (InterceptionStroke*)mouseStroke, _countof(mouseStroke));
//销毁
interception_destroy_context(context);
}
int main(int argc, char *argv[])
{
muni();
// example1();
std::cout<<"--the end ---"<<std::endl;
return 0;
}甚至可以拦截 Ctrl + Alt + Del 牛鼻呀!!