简体   繁体   English

使用C ++在Windows上使用SendInput发送同时的键盘事件

[英]sending simultaneous keyboard events with SendInput on windows in C++

I want to simulate keyboard presses in C++ using SendInput , while single buttons work, trying to send keys simultaneously (shortcuts) doesn't work. 我想使用SendInput模拟C ++中的键盘按键,而单个按钮有效,但尝试同时发送按键(快捷键)却不起作用。

I tried everything, but nothing works, if I send VK_LWIN alone it shows up, but couldn't combine keys simultaneously. 我尝试了所有操作,但没有任何效果,如果我单独发送VK_LWIN ,它会显示,但无法同时组合键。

here's my code: 这是我的代码:

#include <windows.h>
#include <stdlib.h>
#include <stdio.h>

void setInput(INPUT * inp,uint index, WORD keycode,BOOL kUp);
void showRun();

int main(void)
{
    showRun();
    return 0;
}

void setInput(INPUT * inp,uint index, WORD keycode,BOOL kUp)
{
    inp[index].type = INPUT_KEYBOARD;
    inp[index].ki.wVk = keycode;
    inp[index].ki.wScan = MapVirtualKey(keycode, 0);
    inp[index].ki.time = 0;
    inp[index].ki.dwExtraInfo = 0;

    if (kUp == 1)
    {
        inp[index].ki.dwFlags = KEYEVENTF_KEYUP | KEYEVENTF_EXTENDEDKEY;
    }else
    {
        inp[index].ki.dwFlags =  0 | KEYEVENTF_EXTENDEDKEY;
    }
}

//this doesn't do anything
void showRun()
{
    INPUT *inp = (INPUT*) malloc(sizeof(INPUT) * 4);
    memset(inp,0,sizeof(INPUT));

    setInput(inp,0,VK_LWIN,0);
    setInput(inp,1,VK_RBUTTON,0);
    setInput(inp,2,VK_RBUTTON,1);
    setInput(inp,3,VK_LWIN,1);

    SendInput(4,inp,sizeof(INPUT));

    free(inp);
}

while this works fine: 虽然可以正常工作:

void showStart()
{
    INPUT *inp = (INPUT*) malloc(sizeof(INPUT) * 2);
    memset(inp,0,sizeof(INPUT));

    setInput(inp,0,VK_LWIN,0);
    setInput(inp,1,VK_LWIN,1);

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

    free(inp);
}

Thanks in advance for any tip. 在此先感谢您的提示。

啊,愚蠢的我,我应该使用VkKeyScan('r')而不是VK_RBUTTON了,它现在可以工作了!

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

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