Warm tip: This article is reproduced from serverfault.com, please click

emulation-从字符串输入在C ++中使用KeyPress会产生错误

(emulation - Using KeyPress in C++ from string input creates errors)

发布于 2020-12-19 09:27:27

解决:检查此答案中void SendInputString( std::wstring &str)功能

我正在用C ++开发一个可执行文件,可以给它一个字符串作为命令行参数,并且可以模拟键盘来输入它。使用按键。

在命令行上的格式:

{program name} {string to reproduce} {time between individual keypresses in ms}

这是我的代码:

#define WINVER 0x0500
#include <windows.h>

void chartype(char x , INPUT  &input, unsigned int pause_time){

    input.ki.wVk=VkKeyScanA(x);
    //Verifies character and allots the key to the keyboard 

    //cout<<x; //Old check code 


    Sleep(pause_time);
    
    SendInput(1,&input,sizeof(INPUT));
    //To send input  

    return;
}



int main(int argc , char** argv){
    //Initializing a GUI Keyboard Set 
    INPUT input;
    input.type=INPUT_KEYBOARD;

    //Assigning Input Variables
    string str = argv[1];
    size_t n = str.size();
    unsigned int pause_time = stoi(argv[2]);
 
    //Sending individual string characters 
    for(size_t i =0 ; i<n ; ++i){

        chartype(str[i] , input , pause_time);

    }

    //Shutting Keyboard
    input.ki.dwFlags=KEYEVENTF_KEYUP;
    SendInput(1, &input,sizeof(INPUT));
    
    return 0; 
}

但是,该字符串未完全执行。例如,如果我输入字符串PASS_INPUT,它将呈现pass-input同样,输出对是否打开很敏感Caps Lock

我认为它是在发生,因为它仅是模拟按下键盘上的特定键,因此采用默认值,但是我希望它提供确切的输出,例如%9Xa{给出%9Xa{而不是59xa[当前正在发生的。

很抱歉,如果我不能很好地说明问题。我是这种键盘仿真的新手,无法完全理解该怎么做。

我想知道应该进行哪些修改才能按预期呈现字符串。

Questioner
Aniruddh Anna
Viewed
0
Aniruddh Anna 2020-12-19 20:20:07

可以SendInputString( std::wstring &str)这个答案使用功能