Reputation: 331
So I've been trying to make a program that sends a string of keystrokes over to the currently open window and whenever I run the code, it doesn't send whatever I want it to send it sends something completely different(i.e sending bob comes up as 22 or 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;
}
(forgive the horrible formatting)
Upvotes: 0
Views: 1046
Reputation: 409166
The virtual key codes does not generally correspond to the ASCII alphabet.
If you read e.g. this MSDN reference for virtual key-codes you will see that e.g. lower-case 'a'
(which has ASCII value 0x61
) corresponds to VK_NUMPAD1
which is the 1
key on the numeric keyboard.
The upper-case ASCII letters do correspond to the correct virtual key codes, so you need to make all letters upper-case when assigning to bob[i].ki.wVk
. For all other symbols and characters you need to translate the character to the virtual key code.
Upvotes: 2