• Log in or sign up for an account here: https://www.sythe.org/login

Arrow key detection with GetAsyncKeyState

Blupig

BEEF TOILET

$5 USD Donor
Joined
Nov 23, 2006
Sythe Gold
1,609
Messages
7,145
Location
Canada
Discord Unique ID
178533992981594112
Valentine's Singing Competition Winner
Sythe Awards 2015 Winner
Sythe Awards 2016 Winner
Sythe Awards 2020 Winner
Member of the Month Winner
MushyMuncher
Gohan has AIDS
Extreme Homosex
World War 3
I'm LAAAAAAAME
Discord Verified
Off Topic Participant
Hello, here's my code:

Code:
#include <iostream>
#include <Windows.h>
using namespace std;

int main(){

	short key;

	do {

		key = GetAsyncKeyState(VK_UP);
		if (key < 0)
			cout << "Up key pressed" << endl;

	} while (1);

	return 0;
}


It works for its purpose (detecting whether the arrow key is pressed), but when it IS pressed, the output outputs like 50 lines. I just want it to output once. I don't like using sleep, because sleep pauses the current thread. That means that if I use sleep(100); then during that 100ms I cannot push the up arrow key again.

Thanks for any help!

EDIT: Just ended up going with getch() from conio.h, it gives me the results I was looking for.
 
Le Monkey
PHP Shit Show 2k26
Potamus
Prove it!
Sythe Awards 2011 Winner
Tier 1 Prizebox
Tortoise Penis
Trole
UWotM8?
Wait, do you not have an Archer rank?
Code:
while(!GetAsyncKeyState(VK_UP))
    ;
// up key was pressed
 
duelarena.gg
Back
Top