c++ - 发送输入字符串?

标签 c++ string sendmessage sendinput

所以我一直在尝试制作一个程序,将一串击键发送到当前打开的窗口,每当我运行代码时,它不会发送我想要发送的任何内容,而是发送完全不同的东西(即发送 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'(具有 ASCII0x61)对应于 VK_NUMPAD1,即 1数字键盘上的键。

大写 ASCII字母确实对应于正确的虚拟键码,因此在分配给 bob[i].ki.wVk 时,您需要将所有字母设为大写。对于所有其他符号和字符,您需要将字符转换为虚拟键码。

关于c++ - 发送输入字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38283869/

相关文章:

c++ - 如何将 boost::asio::write 的缓冲区存储为shared_ptr?

javascript - 从字符串中获取字符串对

PHP:如何在随机位置的字符串中添加随机字符

javascript - Chrome API : sendMessage not receiving properly from content script to background. js

c++ - 使用 SendMessage() 的鼠标单击不会移交给窗口

c++ - 使用 boost::property_tree::string_path 访问值

c++ - 2013 年的 boost bimap 和 multi_index_container 调试器可视化工具

c++ - 类设计: how to return a shared_ptr: reference or copy

java - 如果字符串中有其他字符,则将字符串转换为 int

c++ - 用户定义的消息