I want to use the SendInput() function to automate pressing some keys.
My code:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <Windows.h>
using std::cin;
using std::cout;
using std::string;
void presskeys(string s){
INPUT ip;
ip.ki.wScan = 0;
ip.ki.time = 0;
ip.ki.dwExtraInfo = 0;
HKL kbl = GetKeyboardLayout(0);
for (unsigned int i = 0; i < s.length(); ++i) {
ip.type = INPUT_KEYBOARD;
char c = s[i];
int vk = VkKeyScanEx((WCHAR)c, kbl);
ip.ki.wVk = vk;
SendInput(1, &ip, sizeof(ip));
ip.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &ip, sizeof(ip));
}
}
int main()
{
cout << "Enter a string!\n";
string str;
cin >> str;
Sleep(5000);
presskeys(str);
cout << "Done!\n";
return 0;
}
I boot up the program and enter a string, only for nothing to happen. Where did it go wrong?