Arrow key detection with GetAsyncKeyState

Discussion in 'Programming General' started by Blupig, Oct 26, 2011.

Arrow key detection with GetAsyncKeyState
  1. Unread #1 - Oct 26, 2011 at 11:00 AM
  2. Blupig
    Joined:
    Nov 23, 2006
    Posts:
    7,145
    Referrals:
    16
    Sythe Gold:
    1,609
    Discord Unique ID:
    178533992981594112
    Valentine's Singing Competition Winner Member of the Month Winner MushyMuncher Gohan has AIDS Extreme Homosex World War 3 I'm LAAAAAAAME
    Off Topic Participant

    Blupig BEEF TOILET
    $5 USD Donor

    Arrow key detection with GetAsyncKeyState

    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.
     
  3. Unread #2 - Oct 28, 2011 at 2:50 PM
  4. Govind
    Joined:
    Apr 22, 2005
    Posts:
    7,825
    Referrals:
    13
    Sythe Gold:
    23
    Prove it! Trole Tier 1 Prizebox Tortoise Penis Le Monkey UWotM8? Wait, do you not have an Archer rank? Potamus

    Govind The One Musketeer
    Mudkips Highly Respected Retired Administrator

    Arrow key detection with GetAsyncKeyState

    Code:
    while(!GetAsyncKeyState(VK_UP))
        ;
    // up key was pressed
     
< AuthKreator - creates nexus auths | Matlab? >

Users viewing this thread
1 guest


 
 
Adblock breaks this site