简体   繁体   English

键盘模拟失败

[英]Keyboard simulation fail

I am making a program and i need it to type a random letter from an array. 我正在制作一个程序,我需要它从数组中键入一个随机字母。 This is how i tried to do it: 这就是我试图做到的方式:

{
srand ( time(NULL) );
WORD arrayNum[4] = {0x41, 0x42, 0x43, 0x44};
int RandIndex = rand() % 4;

INPUT ip;
ip.type = INPUT_KEYBOARD;
ip.ki.wScan = 0;
ip.ki.time = 0;
ip.ki.dwExtraInfo = 0;
ip.ki.wVk = arrayNum; 
ip.ki.dwFlags = 0; 
SendInput(1, &ip, sizeof(INPUT));
ip.ki.dwFlags = KEYEVENTF_KEYUP; 
SendInput(1, &ip, sizeof(INPUT));
}

I must say i am new with this and i don't really know what should i do. 我必须说我对此很陌生,我真的不知道该怎么办。 The error i get says: 我得到的错误是:

invalid conversion from 'WORD* {aka short unsigned int*}' to 'WORD {aka short unsigned int}' [-fpermissive]

This confuses me a lot.. 这让我很困惑。

I think the issue here is in this line: 我认为这里的问题是这样的:

ip.ki.wVk = arrayNum;

Here, the wVk field is supposed to be a WORD representing the virtual key code to send . 在这里, wVk字段应该是代表要发送的虚拟键码的WORD However, you've assigned it arrayNum , which is an array of WORD s. 但是,您已经arrayNum分配了arrayNum ,它是WORD数组 Based on your code, I think you meant 根据您的代码,我认为您的意思是

ip.ki.wVk = arrayNum[RandIndex];

since you weren't using RandIndex anywhere. 因为您没有在任何地方使用RandIndex Going forward, I'd recommend compiling with the warning level turned all the way up, since it probably would have flagged the non-use of RandIndex in the code. 展望未来,我建议您将警告级别提高到最高水平,因为它可能会标记代码中未使用RandIndex

(Also, if you do ask more questions on Stack Overflow, please try to include more information about the error message. I had to look up the docs to determine which line that particular error occurred on. It's a lot easier for people to help out if they can see the specific line where things went wrong.) (此外,如果您确实对Stack Overflow提出了更多问题,请尝试包含有关错误消息的更多信息。我不得不查找文档来确定发生该特定错误的那一行。人们更容易地提供帮助如果他们可以看到发生错误的特定行。)

You are passing the actual array itself to wVk when you should be passing a specific element of the array instead: 您应该将实际数组本身传递给wVk ,而不是传递数组的特定元素:

ip.ki.wVk = arrayNum[RandIndex]; 

That being said, I would suggest using the KEYEVENTF_UNICODE flag when sending text characters with SendInput() , don't use virtual key codes. 话虽这么说,我建议在使用SendInput()发送文本字符时使用KEYEVENTF_UNICODE标志,不要使用虚拟键代码。 And you should pass 2 INPUT structures in one call to SendInput() , like @IInspectable suggested. 并且您应该在一次调用SendInput()传递2个INPUT结构,如建议的@IInspectable。 Don't call SendInput() twice for each INPUT , that defeats some of the benefits of using SendInput() : 不要为每个INPUT两次调用SendInput() ,这会SendInput()使用SendInput()一些好处:

srand ( time(NULL) );
LPCWSTR chars = L"ABCD";
int RandIndex = rand() % 4;

INPUT ip[2];
memset(ip, 0, sizeof(ip));

ip[0].type = INPUT_KEYBOARD;
ip[0].ki.wScan = (WORD) chars[RandIndex]; 
ip[0].ki.dwFlags = KEYEVENTF_UNICODE;

memcpy(&ip[1], &ip[0], sizeof(INPUT));
ip[1].ki.dwFlags |= KEYEVENTF_KEYUP; 

SendInput(2, ip, sizeof(INPUT));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM