C++, How do you detect when the user presses the up, down, left, or right arrow key?

TGCid

Platinum Member
Oct 9, 1999
2,201
0
0
I am coding an arrow key driven menu and would like to know how to detect when the user presses the up, down, left, or right arrow key. So let's say that the user presses the down arrow key, the screen would show the cursor to the next button below. If he presses "enter" then it would launch a function.
 

Adrian Tung

Golden Member
Oct 10, 1999
1,370
1
0
If you're writing in plain vanilla C then I think you *might* be able to use getc (or getchar or something similar to that), but my memory of the "old ways" is kinda hazy to be certain.

On the other hand, if you're writing Win32 apps then you should be using the Windows message handlers to deal with user input.


Hope that helps,
:)atwl
 

BFG10K

Lifer
Aug 14, 2000
22,709
3,003
126
Using standard C++, use:

int ch;
ch = cin.get();


The integer ch will have the last character pressed stored in it. Try each of the arrow keys and output the values to the screen.

Once you know the values for each of the keys, you can therafter use simple equality to test whether they've been pressed.