Jump to content
  • Advertisement
Sign in to follow this  
Bovine13

checking to see if the arrow keys are pressed

This topic is 4664 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'm working on my first game, which is text-based. I would rather have the user move their character by the arrow keys, than by WASD or something else. The only way I know how to read input from the keyboard is through reading in a char and then using a switch statement to process it. In C++, how do I read in the arrow keys (as in, whether or not they have been pressed or not)? Thanks,

Share this post


Link to post
Share on other sites
Advertisement
C++ by itself does not recognize input other than that which can be translated into character values. Therefore in order to recognize things like arrow keys you need to use either use operating system dependent functions or an outside library for input.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You can use getch() to determine if the arrow keys have been hit.
you need to call it twice on certain situations.


int ch, extended;
cout << "enter q to quit.\n";
while ((ch = getch()) != 'q'){
if (ch == 'q'){break;}
if (ch == 0) extended = getch(); else extended = 0;

cout << int(ch) << " " << ch << " Extended: "
if (ch == 0){ cout << int(extended);}
cout << endl;
}
// Note that this was not tested but it should work.



If you want you can put that into a function like int GetKey(int& key, int& extended)

Hit the arrow keys to see what the extended code is for those and just check to see if (ch == 0 && extended == ???)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!