0

the way I have it now when I press the key it keeps doing the code until I press the key again but I want it to do the code once when I press the key

void test() {
bool active = true;
int num = 0;
while (active) {
    if (GetKeyState(VK_PRIOR)) {
        num++;
        cout << num << endl;
    }
    if (GetKeyState(VK_NEXT)) {
        num--;
        cout << num << endl;
    }
}
Lintee
  • 1
  • 1
  • 2
    Your `while` loop's condition is based on `active`, but you never change `active`... – ChrisMM May 09 '22 at 12:34
  • Check specifically for key *up* or *down* events. Perform the action only on one of those events. – Some programmer dude May 09 '22 at 12:35
  • 1
    Did you mean GetAsyncKeyState? Your loop doesn't process window messages, so GetKeyState which returns the key state at the last message is not helpful. – user253751 May 09 '22 at 12:40
  • @user253751 yes that makes it so it only does it when im pressing the key but how can i make it so it does the code once like if i press Page Up key it adds one to the "num" variable or if i press page down it subtracts 1 – Lintee May 09 '22 at 13:10
  • 1
    Apart from anything else, don't busy loop. [Write a proper Windows program](https://learn.microsoft.com/en-us/windows/win32/learnwin32/window-messages) instead. – Paul Sanders May 09 '22 at 13:12

0 Answers0