windows原生的模拟按键

本文详细介绍了在C++中使用WindowsAPI和模拟输入库实现模拟按下特定键(如K或E)的多线程代码,适用于Android和Windows系统。展示了如何在多个线程中独立发送按键事件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//#include <iostream>
//#include <stdlib.h>
//#include <stdio.h>
//#include <thread>
//using namespace std;
//using namespace this_thread;
//void Key();
//void Key() {
//	while (true) {
//		system("input keyevent 'KEYCODE_K'");
//		sleep_for(0.9s);
//	}
//}
//int main()
//{
//	thread th[10];
//	for (int i = 0; i < 10; i++) {
//		cout << "启动第" << i << "个线程\n";
//		th[i] = thread(Key);
//		th[i].detach();
//		sleep_for(1s);
//	}
//	cout << "所有线程启动完成";
//	while (true) {}
//}

//上面是安卓系统模拟按键K,下面是windows系统模拟按键K
//#include <iostream>
//#include <thread>
//#include <Windows.h>
//
//void simulateKeyPress(int keyCode) {
//    INPUT input;
//    input.type = INPUT_KEYBOARD;
//    input.ki.wVk = keyCode;
//    input.ki.dwFlags = 0;
//    input.ki.time = 0;
//    input.ki.dwExtraInfo = 0;
//
//    SendInput(1, &input, sizeof(INPUT));
//}
//
//
//void Key();
//
//void Key() {
//    while (true) {
//        INPUT input;
//        input.type = INPUT_KEYBOARD;
//        input.ki.wVk = 'K';
//        input.ki.dwFlags = 0;
//        input.ki.time = 0;
//        input.ki.dwExtraInfo = 0;
//
//        SendInput(1, &input, sizeof(INPUT));
//        std::this_thread::sleep_for(std::chrono::milliseconds(900));
//    }
//}
//
//
//int main() {
//    std::thread th[10];
//
//    for (int i = 0; i < 10; i++) {
//        std::cout << "启动第" << i << "个线程\n";
//        th[i] = std::thread(Key);
//        th[i].detach();//独立于主线程运行
//        std::this_thread::sleep_for(std::chrono::seconds(1));
//    }
//
//    std::cout << "所有线程启动完成";
//
//    while (true) {}
//
//    return 0;
//}

//#include <iostream>
//#include <thread>
//#include <Windows.h>
#include <iostream>
#include <Windows.h>

void simulateKeyPress(int keyCode) {
//#include <iostream>
//#include <Windows.h>
    INPUT input;
    input.type = INPUT_KEYBOARD;
    input.ki.wVk = keyCode;
    input.ki.dwFlags = 0;
    input.ki.time = 0;
    input.ki.dwExtraInfo = 0;

    SendInput(1, &input, sizeof(INPUT));
}

int main() {
    // 模拟按下 'K' 键
    for (size_t i = 0; i < 10; i++)
    {
        simulateKeyPress('E');
        Sleep(1000);
    }
    return 0;
}```

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值