Sign in to follow this  

checking to see if the arrow keys are pressed

This topic is 4358 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
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

This topic is 4358 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this