GetAsyncKeyState

This is a discussion on GetAsyncKeyState within the Windows Programming forums, part of the Platform Specific Boards category; Code: #include <iostream> #include <stdlib.h> #include <windows.h> double ourNum = 1.00; int main ( ) { while ( GetAsyncKeyState(VK_UP) == ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    141

    GetAsyncKeyState

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <windows.h>
    
    
    double ourNum = 1.00;
    
    int main ( )
    {
    
    
    	while ( GetAsyncKeyState(VK_UP) == 1 )
    	{
    		
    		std:: cout << ourNum++ << std:: endl;
    	
    	}
    
    	std:: cin.get();
    
    }
    The while loop, goes through GetAsyncKeyState each time, and finds if its 1, if it is then it increments our type double ourNum, but.. nothing is outputting..

  2. #2
    Registered User
    Join Date
    May 2008
    Posts
    141
    Weird, it some how returns a number around -32767 or so.

  3. #3
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    First, never compare GetAsyncKeyState() return value with 1, because it does not return a boolean value, but a series of flags. Just check if it is nonzero. And anyway, even if that would have worked, your loop breaks after the first round, because GetAsyncKeyState returns nonzero when the key was not pressed after the last call. That means, this loop can only execute 1 time.

    I think you meant:
    Code:
    while ( true ) {
        if ( !GetAsyncKeyState(VK_UP) ) {
            std::cout << ourNum++ << std::endl;
        }
    }
    But you might also want to add something that breaks the loop, perharps another key.
    Last edited by maxorator; 07-18-2008 at 12:45 AM.

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    141
    Quote Originally Posted by maxorator View Post
    First, never compare GetAsyncKeyState() return value with 1, because it does not return a boolean value, but a series of flags. Just check if it is nonzero. And anyway, even if that would have worked, your loop breaks after the first round, because GetAsyncKeyState returns nonzero when the key was not pressed after the last call. That means, this loop can only execute 1 time.

    I think you meant:
    Code:
    while ( true ) {
        if ( !GetAsyncKeyState(VK_UP) ) {
            std::cout << ourNum++ << std::endl;
        }
    }
    But you might also want to add something that breaks the loop, perharps another key.
    Thanks mister!

    update:
    it didnt work how I wanted it to, I wanted to press up key, and it would return the incremented value.
    Last edited by bobbelPoP; 07-18-2008 at 04:46 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GetAsyncKeyState.
    By kevinawad in forum Windows Programming
    Replies: 9
    Last Post: 11-09-2008, 04:02 PM
  2. GetAsyncKeyState Problem
    By kevinawad in forum C++ Programming
    Replies: 1
    Last Post: 11-07-2008, 05:00 PM
  3. Problems with cin and GetAsyncKeyState
    By KgNe in forum C++ Programming
    Replies: 32
    Last Post: 08-21-2008, 10:00 AM
  4. Infinite Loop with GetAsyncKeyState
    By guitarist809 in forum Windows Programming
    Replies: 1
    Last Post: 04-18-2008, 12:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21