Thread: GetKeyState(VK_.....);

  1. #1
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753

    GetKeyState(VK_.....);

    Here is some code that acts funny, maybe I'm not using the function correctly. What it does is, sometimes it moves on its own (like the key is being pressed) and sometimes it acts like nothing is moving.
    Code:
    void MainLoop() //Main Game Loop
    {
    	switch( LOWORD(GetKeyState (VK_LEFT)) )
    	{
    	case 0:
    		left = true;
    	    break;
    	}
    	switch( LOWORD(GetKeyState (VK_RIGHT)) )
    	{
    	case 0:
    		right = true;
    	    break;
    	}
    	switch( LOWORD(GetKeyState (VK_UP)) )
    	{
    	case 0:
    		up = true;
    	    break;
    	}
    	switch( LOWORD(GetKeyState (VK_DOWN)) )
    	{
    	case 0:
    		down = true;
    	    break;
    	}
    	
    	UpdateShip(left, right, up, down);
    
    	left = false; right = false; up = false; down = false;
    }

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    If you don't mind, just post up the whole program and I'll take a look at it.

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    21
    thats an interesting setup of switch statements

  4. #4
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    lol i know. once i figure out how to use GetKeyState() I'll make it much more efficient.

  5. #5
    Registered User Coder's Avatar
    Join Date
    Aug 2001
    Location
    Cairo, Egypt
    Posts
    128
    [Docs]
    If the function succeeds, the return value specifies the status of the given virtual key. If the high-order bit is 1, the key is down; otherwise, it is up. If the low-order bit is 1, the key is toggled.[/Docs]

    Don't check the return value, check the high-order bit of the return value ( use a logical AND : & )

  6. #6
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    i just used it like:
    Code:
    if(GetKeyState(VK_UP)) 
        //code here

  7. #7
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    I think this would be easier

    Code:
    BYTE     KeyStates[256];
    
    if(!GetKeyboardState(&KeyStates))//error
        return FALSE;
    
    UpdateShip(KeyStates[VK_LEFT] ,KeyStates[VK_RIGHT] ,KeyStates[VK_UP] ,KeyStates[VK_DOWN] );

Popular pages Recent additions subscribe to a feed