Oct 24, 2008 at 10:30pm UTC
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 Oct 24, 2008 at 10:30pm UTC
Oct 24, 2008 at 11:07pm UTC
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 Oct 25, 2008 at 5:49pm UTC
Oct 25, 2008 at 1:21pm UTC
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?
Oct 25, 2008 at 4:53pm UTC
Sometimes I get 224, sometimes I get -32. I honestly have no idea why.
Oct 25, 2008 at 4:56pm UTC
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 Oct 25, 2008 at 4:58pm UTC
Oct 25, 2008 at 5:50pm UTC
I changed it to an unsigned char, which should make it 224.
Oct 25, 2008 at 5:55pm UTC
Yes, i now get the values 72 and 224 on the screen. :)
Last edited on Oct 25, 2008 at 5:56pm UTC
Oct 25, 2008 at 11:30pm UTC
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.