-1

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

2 Answers2

0

You can keep the state of whether it's the first time you've clicked the button or not. 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;
         }
}
Ted Klein Bergman
  • 9,146
  • 4
  • 29
  • 50
  • I tried that the same problem, it keeps sending the same key – Heja Babyj Bibani Jun 10 '20 at 12:44
  • It works on text editor, but in the game that I'm using it for, it keeps pressing Don't know what's going on. The reason I'm doing it is to double the amount of times I press a key in the game, it helps with lag movements. – Heja Babyj Bibani Jun 10 '20 at 12:59
-1

hi you can use this code: SendKeys::Send("{UP}");