Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

DrewSmug

arrow keys

This topic is 5309 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i posted a similar post in another forum but i feel this is the most appropriate forum for my question. i have a function that checks to see if an arrow key was pressed. i call it a bunch of times in the begining just to see if the program would continue when an arrow key wasn''t being pressed. but it stalls every other time the function is called and refused to continue till i actually press an arrow key. then at the end trying to test the same thing i put the function in a loop and hoped i would see the word ''blah'' over and over again except for when i tapped one of the arrow keys. but no it stalls again and refused to proceed in my program till i actually push a button. if any body knows how i could do the same thing but have the function just continue through when no arrow key is pressed. any and all help appreciated, thanks.
#include <iostream>
#include <windows.h>

void blah();

void main(){
	blah();
	std::cout<<"1\n";
	blah();
	std::cout<<"2\n";
	blah();
	std::cout<<"3\n";
	blah();
	std::cout<<"4\n";
	blah();
	std::cout<<"5\n";
	blah();
	std::cout<<"6\n";
	blah();
	std::cout<<"7\n";
	blah();
	std::cout<<"8\n";
	blah();
	std::cout<<"9\n";
	blah();
	std::cout<<"10\n";

	while(true)	
	{	
	blah();
	std::cout<<"blah\n";
	}  


}


void blah()
{
	INPUT_RECORD *str = new INPUT_RECORD[1];
	DWORD num = 0;	

	
		ReadConsoleInput(GetStdHandle(STD_INPUT_HANDLE), str, 1, &num);		
		if(num != 0 && str[0].Event.KeyEvent.bKeyDown)		
		{			
			WORD key = str[0].Event.KeyEvent.wVirtualKeyCode;		
			switch(key)			
			{			
			case VK_UP:				
				{				
					// UP key				

					std::cout << "up\n";		
				} break;		
			case VK_DOWN:		
				{				
					// DOWN key		

					std::cout << "down\n";	
				} break;	
			case VK_LEFT:		
				{				
					// LEFT key		

					std::cout << "left\n";	
				} break;		
			case VK_RIGHT:			
				{				
					// RIGHT key	

					std::cout << "right\n";		
				} break;	
			case VK_ESCAPE:	
				{				
					// Quit		

					exit(false);	
				} break;		
			}	
			num = 0;	
		}

}

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
You could just use GetAsyncKeyState() or a similar function to poll the arrow keys.

Share this post


Link to post
Share on other sites
The problem is that the ReadConsoleInput function doesn't return until you hit a key, causing the stall. AP's solution should work - but check out the documentation of the function at MSDN
There are a couple quirks across different OS's.

[EDIT]
Click'ied the link.

[edited by - HappyDude on April 11, 2004 1:41:01 AM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!