简体   繁体   English

C++ 发送输入()

[英]C++ SendInput()

I want to create a program which sends exactly one key stroke for every key stroke I press.我想创建一个程序,它为我按下的每个击键发送一个击键。 I'm having a problem with this program because it keeps sending keystrokes even after I stopped pressing the key.我在使用该程序时遇到问题,因为即使我停止按键后它仍会继续发送击键。

For example;例如; if pressed "UP", it will continue pressing up until i press another key.如果按下“向上”,它将继续向上按下,直到我按下另一个键。

Can you help me with this please.你能帮我解决这个问题吗?

Thank you谢谢


int main()
{
    // This structure will be used to create the keyboard
    // input event.
    INPUT ip;

    while(1)
    {
             if (GetAsyncKeyState(VK_UP) < 0)
          {
            INPUT ip;
            ip.type = INPUT_KEYBOARD;
            ip.ki.time = 0;
            ip.ki.dwExtraInfo = 0;          
            ip.ki.wVk = 0x26; // virtual-key code for the "UP arrow" key
            ip.ki.dwFlags = 0; // 0 for key press
            SendInput(1, &ip, sizeof(INPUT));
          }

          if (GetAsyncKeyState(VK_DOWN) < 0)
          {
            INPUT ip;
            ip.type = INPUT_KEYBOARD;
            ip.ki.time = 0;
            ip.ki.dwExtraInfo = 0;          
            ip.ki.wVk = 0x28; // virtual-key code for the "UP arrow" key
            ip.ki.dwFlags = 0; // 0 for key press
            SendInput(1, &ip, sizeof(INPUT));


          }

           if (GetAsyncKeyState(VK_RIGHT) < 0)
          {
            INPUT ip;
            ip.type = INPUT_KEYBOARD;
            ip.ki.time = 0;
            ip.ki.wVk = 0;
            ip.ki.dwExtraInfo = 0;          
            ip.ki.wVk = 0x27; // virtual-key code for the "UP arrow" key
            ip.ki.dwFlags = 0; // 0 for key press
            SendInput(1, &ip, sizeof(INPUT));

          }

          if (GetAsyncKeyState(VK_LEFT) < 0)
          {
            INPUT ip;
            ip.type = INPUT_KEYBOARD;
            ip.ki.time = 0;
            ip.ki.dwExtraInfo = 0;          
            ip.ki.wVk = 0x25; // virtual-key code for the "UP arrow" key
            ip.ki.dwFlags = 0; // 0 for key press
            SendInput(1, &ip, sizeof(INPUT));


          }

    }

    // Exit normally
    return 0;
}

You can keep the state of whether it's the first time you've clicked the button or not.您可以保留 state 是否是您第一次单击按钮。 Then you just reset it when you release.然后,您只需在释放时将其重置。 Here's an example for one of the buttons.这是其中一个按钮的示例。

bool up_pressed = false;

int main()
{
         if (GetAsyncKeyState(VK_UP) < 0)
         {
            if (!up_pressed)
            {
                up_pressed = true;
                INPUT ip;
                ip.type = INPUT_KEYBOARD;
                ip.ki.time = 0;
                ip.ki.dwExtraInfo = 0;          
                ip.ki.wVk = 0x26; // virtual-key code for the "UP arrow" key
                ip.ki.dwFlags = 0; // 0 for key press
                SendInput(1, &ip, sizeof(INPUT));
            }
         }
         else 
         {
             up_pressed = false;
         }
}

hi you can use this code: SendKeys::Send("{UP}");您好,您可以使用此代码: SendKeys::Send("{UP}");

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

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