Public Group

checking to see if the arrow keys are pressed

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

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 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 on other sites
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 == ???)

1. 1
Rutin
24
2. 2
3. 3
JoeJ
18
4. 4
5. 5

• 38
• 23
• 13
• 13
• 17
• Forum Statistics

• Total Topics
631706
• Total Posts
3001835
×