C++发送输入()

bq3bfh9z  于 2023-01-28  发布在  其他
关注(0)|答案(2)|浏览(147)

我想创建一个程序,我每按一次键,它就发送一次键击。这个程序有问题,因为即使在我停止按键后,它仍继续发送键击。
例如:如果按下"向上",它将继续按向上,直到我按下另一个键。
你能帮我一下吗?
谢谢

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;
}
uurity8g

uurity8g1#

你可以保持你是否第一次点击按钮的状态。然后你只需要在释放按钮时重置它。下面是其中一个按钮的例子。

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;
         }
}
ar7v8xwq

ar7v8xwq2#

您可以使用以下代码:SendKeys::Send("{UP}");

相关问题