如何在 C++ 中模拟按键

发布于 2024-10-31 06:09:51 字数 116 浏览 4 评论 0原文

我想知道如何在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最后的乘客 2024-11-07 06:09:51

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

It looks like you want to use either SendInput() or keybd_event() (which is an older way of doing the same thing).

固执像三岁 2024-11-07 06:09:51

首先 - 在如何在 C++ 中使用 sendinput 函数上找到这个答案。

看代码部分:

// ...
    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));
// ...

我没明白这个幻数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:

// ...
    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.

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 like VK_A - VK_Z I was looking for in winuser.h header.

那支青花 2024-11-07 06:09:51

我的两分钱。
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!...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文