所以我一直在尝试制作一个程序,将一串击键发送到当前打开的窗口,每当我运行代码时,它不会发送我想要发送的任何内容,而是发送完全不同的东西(即发送 bob 出现 22 或 2/2)
#include <iostream>
#include <vector>
#include <Windows.h>
int SendKeys(const std::string &msg);
int main() {
Sleep(5);
while(true) {
Sleep(500);
SendKeys("iajsdasdkjahdjkasd");
}
std::cin.get();
return 0;
}
int SendKeys(const std::string & msg)
{
std::vector<INPUT> bob(msg.size());
for(unsigned int i = 0; i < msg.size(); ++i)
{
bob[i].type = INPUT_KEYBOARD;
bob[i].ki.wVk = msg[i];
std::cout << bob[i].ki.wVk << std::endl;
auto key = SendInput(1, &bob[i], sizeof(INPUT) /* *bob.size() */);
}
return 0;
}
(请原谅可怕的格式)
最佳答案
虚拟键码一般不对应ASCII字母。
如果您阅读例如this MSDN reference for virtual key-codes你会看到,例如小写的 'a'
(具有 ASCII 值 0x61
)对应于 VK_NUMPAD1
,即 1
数字键盘上的键。
大写 ASCII字母确实对应于正确的虚拟键码,因此在分配给 bob[i].ki.wVk
时,您需要将所有字母设为大写。对于所有其他符号和字符,您需要将字符转换为虚拟键码。
关于c++ - 发送输入字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38283869/