Created
October 22, 2024 13:47
-
-
Save widcardw/ea4ed0290646a441e6759a6f87db9554 to your computer and use it in GitHub Desktop.
Windows 键盘输入模拟
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 仅适用于 Windows,仅适用于美式键盘,请先将输入法切换到英文输入法 | |
// 使用 g++ 编译后运行,使用方式 | |
// .\typewriter.exe <file.txt> | |
// 然后迅速地将光标移动到想要进行输入的窗口 | |
// 仅支持美式键盘上的字符 | |
#include <iostream> | |
#include <windows.h> | |
#include <fstream> | |
#include <sstream> | |
#include <string> | |
#define LC(x,y) if(text[i] == x) { lower_char(y); continue; } | |
#define UC(x,y) if(text[i] == x) { upper_char(y); continue; } | |
void lower_char(int vk) { | |
// 模拟了按下一个按键,相当于按下了小写字母 | |
keybd_event(vk, 0, 0, 0); | |
keybd_event(vk, 0, KEYEVENTF_KEYUP, 0); | |
} | |
void upper_char(int vk) { | |
// 模拟了按下一个按键,相当于按下了大写字母 | |
// 需要模拟用组合键按下:Shift+大写字母 | |
keybd_event(VK_SHIFT, 0, 0, 0); | |
keybd_event(vk, 0, 0, 0); | |
keybd_event(vk, 0, KEYEVENTF_KEYUP, 0); | |
keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0); | |
} | |
std::string readFile(const std::string& filename) { | |
std::ifstream file(filename); | |
std::stringstream buffer; | |
buffer << file.rdbuf(); | |
return buffer.str(); | |
} | |
int main(int argc, char* argv[]) { | |
system("chcp 65001"); | |
// 传入文件名,读取文件内容 | |
std::string text = readFile(argv[1]); | |
std::cout << "5秒钟后开始输入,请将光标移动到指定位置" << std::endl; | |
Sleep(1000); | |
std::cout << "4秒钟后开始输入,请将光标移动到指定位置" << std::endl; | |
Sleep(1000); | |
std::cout << "3秒钟后开始输入,请将光标移动到指定位置" << std::endl; | |
Sleep(1000); | |
std::cout << "2秒钟后开始输入,请将光标移动到指定位置" << std::endl; | |
Sleep(1000); | |
std::cout << "1秒钟后开始输入,请将光标移动到指定位置" << std::endl; | |
Sleep(1000); | |
for (int i = 0; i < text.size(); i++) { | |
Sleep(10); | |
if (text[i] >= 'a' && text[i] <= 'z') { | |
lower_char(text[i] - 'a' + 'A'); | |
continue; | |
} | |
if (text[i] >= 'A' && text[i] <= 'Z') { | |
upper_char(text[i] - 'A' + 'A'); | |
continue; | |
} | |
if (text[i] >= '0' && text[i] <= '9') { | |
lower_char(text[i]); | |
continue; | |
} | |
// https://learn.microsoft.com/zh-cn/windows/win32/inputdev/virtual-key-codes | |
LC('.', VK_OEM_PERIOD); | |
LC(',', VK_OEM_COMMA); | |
LC('\n', VK_RETURN); | |
LC('\t', VK_TAB); | |
LC(' ', VK_SPACE); | |
LC('/', VK_OEM_2); | |
LC('\\', VK_OEM_5); | |
LC('`', VK_OEM_3); | |
UC('~', VK_OEM_3); | |
LC('\'', VK_OEM_7); | |
UC('"', VK_OEM_7); | |
UC('|', VK_OEM_5); | |
LC('[', VK_OEM_4); | |
LC(']', VK_OEM_6); | |
UC('{', VK_OEM_4); | |
UC('}', VK_OEM_6); | |
LC('-', VK_OEM_MINUS); | |
UC('_', VK_OEM_MINUS); | |
LC('=', VK_OEM_PLUS); | |
UC('+', VK_OEM_PLUS); | |
LC(';', VK_OEM_1); | |
UC(':', VK_OEM_1); | |
UC('!', '1'); | |
UC('@', '2'); | |
UC('#', '3'); | |
UC('$', '4'); | |
UC('%', '5'); | |
UC('^', '6'); | |
UC('&', '7'); | |
UC('*', '8'); | |
UC('(', '9'); | |
UC(')', '0'); | |
UC('>', VK_OEM_PERIOD); | |
UC('<', VK_OEM_COMMA); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment