Arrow Key Identifiers

What is the name to identify an arrow key? For example, to have a switch statement with a case of an arrow key when using getch();

I was thinking it would be something like this, but I don't know how to identify an arrow key:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <conio.h>
using namespace std;

int main() {
     char cInput = getch();
     switch( cInput ) {
          case ARROW_KEY_UP:
               cout << "Up arrow key!";
               break;
     }
return 0;
}
Last edited on
getch() behaves very strangely. The up arrow key returns two values, in fact, first 224, then 72. Try running this program and you'll see what I mean...
1
2
3
4
5
6
7
8
9
10
11
12
#include <conio.h>
#include <iostream>

int main()
{
	unsigned char LOL;
	while(1){
	LOL = getch();
	std::cout << (int)LOL << ' ' << LOL << '\n';
	}
	return 0;
}

Hopefully that will let you know what you need...
Last edited on
if i use your code above, the up arrow key returns:
-32 รณ
72 H

Do you know wy? Is this different by every (type) computer?
Sometimes I get 224, sometimes I get -32. I honestly have no idea why.
Oh. I figured it out.
getch() returns 224. But if you stick it in a char and (int) it, it converts to -32.

EDIT: Yep, (int)((char)224) returns -32.
Last edited on
Oke, tanks
I changed it to an unsigned char, which should make it 224.
Yes, i now get the values 72 and 224 on the screen. :)
Last edited on
Many of the "special" keys on a keyboard -- Up, Down, Left, Right, Home, End, Function keys, etc. actually return two scan codes from the keyboard controller back to the CPU. The "standard" keys all return one. So yes, if you want to check for special keys, you'll need to call getch() twice. Google "keyboard scan codes" for more info.
Topic archived. No new replies allowed.