你能解释一下 C++ 的每一行是什么吗?函数是做什么的?它发送击键,但我对如何发送击键感到困惑

发布于 2024-12-23 21:08:50 字数 1310 浏览 7 评论 0 原文

我正在编写一个程序将击键发送到另一个窗口,并且一切正常,但我必须上网并找到一个函数来完成击键部分本身。它有效,但我不知道它实际上在做什么。有人可以评论这个函数的每一行来解释它在做什么吗?

void GenerateKey(int vk, BOOL bExtended) {

    KEYBDINPUT  kb = {0};
    INPUT       Input = {0};

    /* Generate a "key down" */
    if (bExtended) { kb.dwFlags  = KEYEVENTF_EXTENDEDKEY; }
    kb.wVk  = vk;
    Input.type  = INPUT_KEYBOARD;
    Input.ki  = kb;
    SendInput(1, &Input, sizeof(Input));

    /* Generate a "key up" */
    ZeroMemory(&kb, sizeof(KEYBDINPUT));
    ZeroMemory(&Input, sizeof(INPUT));
    kb.dwFlags  =  KEYEVENTF_KEYUP;
    if (bExtended) { kb.dwFlags |= KEYEVENTF_EXTENDEDKEY; }
    kb.wVk = vk;
    Input.type = INPUT_KEYBOARD;
    Input.ki = kb;
    SendInput(1, &Input, sizeof(Input));

    return;
}

下面是调用它的示例:

GenerateKey('C', FALSE);    // Sends keystroke 'c'

这会发送击键“c”。

此函数仅适用于大写字母,并且似乎仅适用于特定的十六进制代码。例如,要发送回车符(输入键),请执行以下调用:

GenerateKey(0x0D, FALSE);   // Sends carriage return

但是,如果我尝试使用这些调用中的任何一个发送问号(十六进制 0x3F),则不会发生任何事情:

GenerateKey(0x3F, FALSE);  // Nothing happens
GenerateKey('?', FALSE);   // Nothing happens

谁能明白为什么这些不起作用?

另外,有人可以解释第二个参数 BOOL bExtended 的用途吗?在 TRUE 和 FALSE 之间进行更改似乎对其发送的击键没有影响。

I was making a program to send keystrokes to another window and I got it all working, but I had to go online and find a function to do the keystroke portion itself. It works but I have no idea what it is actually doing. Can someone comment each line of this function explaining what it's doing?

void GenerateKey(int vk, BOOL bExtended) {

    KEYBDINPUT  kb = {0};
    INPUT       Input = {0};

    /* Generate a "key down" */
    if (bExtended) { kb.dwFlags  = KEYEVENTF_EXTENDEDKEY; }
    kb.wVk  = vk;
    Input.type  = INPUT_KEYBOARD;
    Input.ki  = kb;
    SendInput(1, &Input, sizeof(Input));

    /* Generate a "key up" */
    ZeroMemory(&kb, sizeof(KEYBDINPUT));
    ZeroMemory(&Input, sizeof(INPUT));
    kb.dwFlags  =  KEYEVENTF_KEYUP;
    if (bExtended) { kb.dwFlags |= KEYEVENTF_EXTENDEDKEY; }
    kb.wVk = vk;
    Input.type = INPUT_KEYBOARD;
    Input.ki = kb;
    SendInput(1, &Input, sizeof(Input));

    return;
}

Here's an example of a call to it:

GenerateKey('C', FALSE);    // Sends keystroke 'c'

This sends the keystroke 'c'.

This function only works with capital letters and seems to only work with specific hex codes. For example, to send a carriage return (enter key), here is the call:

GenerateKey(0x0D, FALSE);   // Sends carriage return

However, if I try sending a question mark (hex 0x3F) with either of these calls, nothing happens:

GenerateKey(0x3F, FALSE);  // Nothing happens
GenerateKey('?', FALSE);   // Nothing happens

Can anyone see why those wouldn't work?

Also, can someone explain what the second argument, BOOL bExtended, is for? Changing between TRUE and FALSE seems to make no difference in the keystrokes it sends.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

醉生梦死 2024-12-30 21:08:50

您的 GenerateKey 函数在内部使用 Win32 API 中的 SendInput 函数,该函数合成键盘输入。

您可以通过阅读 该函数的 MSDN 文档

vk 参数是一个虚拟键码,列出此处

如果 bExtended 参数为 TRUE,则设置 KEYEVENTF_EXTENDEDKEY 标志,根据 文档 表示“扫描代码前面有一个前缀字节,其值0xE0(224)”。

Your GenerateKey function makes internal use of the SendInput function from the Win32 API, which synthesizes keyboard input.

You can find all of the information you need to understand how that function works and how to call it by reading the MSDN documentation for that function.

The vk parameter is a virtual key code, listed here.

If the bExtended parameter is TRUE, then the KEYEVENTF_EXTENDEDKEY flag is set, which according to the documentation means that "the scan code was preceded by a prefix byte that has the value 0xE0 (224)".

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