简体   繁体   English

如何在 C++ 中模拟按键操作

[英]How to simulate a key press in C++

I was wondering how can I simulate a key depression in C++.我想知道如何在 C++ 中模拟一个关键的抑郁症。 Such as having code that when I run the program it presses the letter "W" key.例如,当我运行程序时,它按下字母“W”键的代码。 I don't want to be displaying it in a console window I just want it to display the "W" key every time I click on a text field.我不想在控制台窗口中显示它,我只想在每次单击文本字段时显示“W”键。 Thanks!谢谢!

Note: I am not trying to make a spammer.注意:我不是要制造垃圾邮件发送者。

看起来您想使用SendInput()keybd_event() (这是做同样事情的旧方法)。

First - find this answer on how to use sendinput function in C++ .首先 - 找到有关如何在 C++ 中使用 sendinput 函数的答案。

Look at the code section:看代码部分:

// ...
    INPUT ip;
// ...
    // Set up a generic keyboard event.
    ip.type = INPUT_KEYBOARD;
    ip.ki.wScan = 0; // hardware scan code for key
    ip.ki.time = 0;
    ip.ki.dwExtraInfo = 0;

    // Press the "A" key
    ip.ki.wVk = 0x41; // virtual-key code for the "a" key
    ip.ki.dwFlags = 0; // 0 for key press
    SendInput(1, &ip, sizeof(INPUT));
// ...

I didn't understand where the magic number 0x41 came from.我不明白神奇数字0x41是从哪里来的。

Go to SendInput documentation page .转到SendInput 文档页面 Still don't understand where's the 0x41 .仍然不明白0x41在哪里。

Go to INPUT documentation and from there to KEYBDINPUT documentation.转到INPUT文档,然后从那里转到KEYBDINPUT文档。 Still no magic 0x41 .仍然没有魔法0x41

Finally go to Virtual-Key Codes page and understand that Microsoft has given the names for Ctrl (VK_CONTROL), Alt (VK_MENU), F1-F24 (VK_F1 - VK_F24, where are 13-24 is a mystery), but forgot to name characters.最后去Virtual-Key Codes页面,了解到微软已经给了 Ctrl (VK_CONTROL), Alt (VK_MENU), F1-F24 (VK_F1 - VK_F24, where are 13-24 is a secret) 的名字,但是忘了命名字符. Actual characters have codes ( 0x41-0x5A ), but don't have names like VK_A - VK_Z I was looking for in winuser.h header.实际字符有代码( 0x41-0x5A ),但没有像我在winuser.h标头中寻找的VK_A - VK_Z类的名称。

How can I fire a key press or mouse click event without touching any input device at system level? 如何在不触摸系统级别任何输入设备的情况下触发按键或单击鼠标事件?

Oh, by the way, you probably don't want to do this stuff; 哦,顺便说一句,您可能不想这样做。 if you are simulating key-presses to make your application behave the way you want it to, then you might want to rethink how you are designing your application. 如果要模拟按键以使应用程序表现出所需的行为,那么您可能需要重新考虑如何设计应用程序。 In my experience sending key presses and intercepting them causes nothing but woes. 以我的经验,发送按键和拦截按键只会造成麻烦。

It is a virtual-key code for the key "A" as the comment says.正如注释所说,它是键“A”的虚拟键代码。 It is just what Windows' developers decided the numeration shall be.这正是 Windows 开发人员决定的编号。 Here is a link with all the keys where 0x41 is documented as A. 是包含所有键的链接,其中0x41被记录为 A。

My two cents in this.我的两分钱在这。 Character "A" in ASCII is 65 in decimal. ASCII 中的字符“A”是十进制的 65。 But in HEX is 41. Hence, 0x41但在十六进制中是 41。因此,0x41

Look here for the ASCII table http://www.asciitable.com/在此处查看 ASCII 表http://www.asciitable.com/

Maybe you would be interested in the SCAN codes as well, not just in ASCII.也许您也会对 SCAN 代码感兴趣,而不仅仅是 ASCII。 SCAN codes can even tell (and simulate) when the key is downpressed, but also when that key is actually released. SCAN 代码甚至可以判断(和模拟)按键何时被按下,以及该按键何时被真正释放。 SCAN codes are usually followed as well by the ASCII codes in the "input buffer". “输入缓冲区”中的 ASCII 代码通常也跟在 SCAN 代码之后。 Here, about the "input buffer" (which is actually a FIFO list in RAM): How to clear input buffer in C?在这里,关于“输入缓冲区”(实际上是 RAM 中的 FIFO 列表): 如何清除 C 中的输入缓冲区?

Here is a small example: http://csourcecodes.blogspot.com/2014/08/c-program-to-display-scan-and-ascii.html这是一个小例子: http : //csourcecodes.blogspot.com/2014/08/c-program-to-display-scan-and-ascii.html

If you're using Windows - possibly your best approach would be the "SendInput" mentioned earlier.如果您使用的是 Windows - 最好的方法可能是前面提到的“SendInput”。 Here is a small doc about that: https://batchloaf.wordpress.com/2012/04/17/simulating-a-keystroke-in-win32-c-or-c-using-sendinput/这是一个关于此的小文档: https : //batchloaf.wordpress.com/2012/04/17/simulating-a-keystroke-in-win32-c-or-c-using-sendinput/

Hope it helps you - at least a bit!... :)希望它可以帮助您 - 至少有一点!... :)

success!...成功!...

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

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