如何在 C++ 中模拟按键
我想知道如何在 C++ 中模拟按键按下。例如,当我运行程序时,它会按下字母“W”键。我不想在控制台窗口中显示它,我只是希望每次单击文本字段时它都显示“W”键。谢谢!
注意:我并不是想制造垃圾邮件发送者。
I was wondering how can I simulate a key depression in C++. Such as having code that when I run the program it presses the letter "W" key. 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. Thanks!
Note: I am not trying to make a spammer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看起来您想使用
SendInput ()
或 < code>keybd_event() (这是做同样事情的旧方法)。It looks like you want to use either
SendInput()
orkeybd_event()
(which is an older way of doing the same thing).首先 - 在如何在 C++ 中使用 sendinput 函数上找到这个答案。
看代码部分:
我没明白这个幻数
0x41
是从哪里来的。转到 SendInput 文档页面。还是不明白
0x41
在哪里。转到 INPUT 文档并从那里到 KEYBDINPUT 文档。仍然没有魔法
0x41
。最后转到虚拟键代码页面并据了解,微软已经给了Ctrl(VK_CONTROL)、Alt(VK_MENU)、F1-F24(VK_F1 - VK_F24,其中13-24是个谜)的名称,但忘记命名字符。实际字符有代码 (
0x41-0x5A
),但没有像VK_A - VK_Z
我在winuser.h
标头中查找的名称。First - find this answer on how to use sendinput function in C++.
Look at the code section:
I didn't understand where the magic number
0x41
came from.Go to SendInput documentation page. Still don't understand where's the
0x41
.Go to INPUT documentation and from there to KEYBDINPUT documentation. Still no magic
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. Actual characters have codes (
0x41-0x5A
), but don't have names likeVK_A - VK_Z
I was looking for inwinuser.h
header.我的两分钱。
ASCII 中的字符“A”十进制为 65。但在十六进制中是 41。因此,0x41
在此处查找 ASCII 表
http://www.asciitable.com/
也许您也会对 SCAN 代码感兴趣,而不是只是用 ASCII 表示。 SCAN 代码甚至可以告诉(并模拟)何时按下该键,以及何时实际释放该键。 SCAN 代码之后通常还跟有“输入缓冲区”中的 ASCII 代码。
这里,关于“输入缓冲区”(实际上是RAM中的一个FIFO列表):
如何清除 C 中的输入缓冲区?
这是一个小示例:
http://csourcecodes.blogspot。 com/2014/08/c-program-to-display-scan-and-ascii.html
如果您使用的是 Windows - 可能您最好的方法是前面提到的“SendInput”。
这是一个关于此的小文档:
https ://batchloaf.wordpress.com/2012/04/17/simulated-a-keytrip-in-win32-c-or-c-using-sendinput/
希望它可以帮助您 - 至少有一点! .. :)
成功!...
My two cents in this.
Character "A" in ASCII is 65 in decimal. But in HEX is 41. Hence, 0x41
Look here for the ASCII table
http://www.asciitable.com/
Maybe you would be interested in the SCAN codes as well, not just in ASCII. SCAN codes can even tell (and simulate) when the key is downpressed, but also when that key is actually released. SCAN codes are usually followed as well by the ASCII codes in the "input buffer".
Here, about the "input buffer" (which is actually a FIFO list in RAM):
How to clear input buffer in C?
Here is a small example:
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.
Here is a small doc about that:
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!...