简体   繁体   English

在系统范围内按SendInput

[英]Making a system-wide key press SendInput

I'm trying to make a program which sends two additional key presses for every left mouse button press I make. 我正在尝试制作一个程序,每次我按下鼠标左键时,它都会发送两次额外的按键。 This all works fine except when I'm in another program (in my case it's a game), then it does sense the left mouse button being pressed but it does not press the additional two virtual keys for me. 一切正常,除非当我在另一个程序中(在我的情况下是游戏)时,它确实会感觉到按下了鼠标左键,但没有为我按下另外两个虚拟键。 The entire code: 整个代码:

#include <Windows.h>
#include <iostream>
int main ()
{
INPUT ip;
bool press = false;
int i = 0;
while ( true )
{
    if ( GetKeyState( VK_LBUTTON) < 0 & !press )
    {
        std::cout << "press" << i++ << "\n";
        // PRESS F8
        ip.type = INPUT_KEYBOARD;
        ip.ki.wScan = 0x42; // hardware scan code for key
        ip.ki.time = 0;
        ip.ki.dwExtraInfo = 0;
        ip.ki.wVk = 0x77; // virtual-key code 

        ip.ki.dwFlags = 0; // 0 for key press
        SendInput(1, &ip, sizeof(INPUT));
        if (GetKeyState( VK_F8) < 0) { std::cout << "press f8 \n";}
        ip.ki.dwFlags = KEYEVENTF_KEYUP; // KEYEVENTF_KEYUP for key release
        SendInput(1, &ip, sizeof(INPUT));

        ip.ki.dwFlags = 0; // 0 for key press
        SendInput(1, &ip, sizeof(INPUT));
        ip.ki.dwFlags = KEYEVENTF_KEYUP; // KEYEVENTF_KEYUP for key release
        SendInput(1, &ip, sizeof(INPUT));

        press = true;
    }
    if ( GetKeyState( VK_LBUTTON) >= 0 )
    {
        press = false;
    }
}
return(0);
}

Now I have read everything I could find regarding this, which isn't all that much btw, and I think it has something to do with using scancodes instead of virtualkeycodes. 现在,我已经阅读了所有可以找到的有关此内容的信息,但并没有那么多,而且我认为这与使用扫描代码而不是virtualkeycode有关。 The problem is that when I make this ip.ki.wScan = 0; 问题是当我使这个ip.ki.wScan = 0时; and ip.ki.wVk = 0x77; 和ip.ki.wVk = 0x77; it will do it right but not inside the game, same when I use both scancode and VKcode. 当我同时使用scancode和VKcode时,它会正确执行但不会在游戏中执行。 But when VKcode is zero it stops pressing the F8 key even whem I'm not in the game. 但是当VKcode为零时,即使我不在游戏中,它也会停止按F8键。

So my question is: How do I make a system-wide virtual key press that will also work when I'm not in desktop? 所以我的问题是:如何在不使用台式机的情况下进行系统范围的虚拟按键操作?

Ok I found out how to use the scancodes to make the virtual keypress 好的,我发现了如何使用扫描代码进行虚拟按键

// PRESS F8
        ip.type = INPUT_KEYBOARD;
        ip.ki.wScan = MapVirtualKey(VK_F8, 0); // hardware scan code for key
        ip.ki.time = 0;
        ip.ki.dwExtraInfo = 0;
        ip.ki.wVk = 0;//0x77; // virtual-key code 

        ip.ki.dwFlags = KEYEVENTF_SCANCODE; // 0 for key press
        SendInput(1, &ip, sizeof(INPUT));
        if (GetKeyState( VK_F8) < 0) { std::cout << "press f8 \n";}
        ip.ki.dwFlags = KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP; // KEYEVENTF_KEYUP for key release
        SendInput(1, &ip, sizeof(INPUT));

The dwFlags should be KEYEVENTF_SCANCODE else it will automatically use the VK code to determine which is supposed to be pressed. dwFlags应该是KEYEVENTF_SCANCODE,否则它将自动使用VK代码来确定应该按下哪个。

Now it still does not send the virtual key press to the game I'm running. 现在它仍然不会将虚拟按键发送给我正在运行的游戏。 Whenever I'm in this game it just doesn't work, how can I fix this?? 每当我在此游戏中时,它都将不起作用,该如何解决?

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

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