Arrow Keys and getch

Started by
15 comments, last by Eric Poitras 17 years, 4 months ago
Well since I'm getting a 224 as the first code where KulSeran was getting a 0, and you are getting different results from both of us, I can only assume that the results of _getch() are different with different hardware or something.

Unless anyone can explain these different results better, I can only suggest that you use the Win32 API key event code I posted first. That should work the same on any windows machine.

All very strange though.
Advertisement
Quote:Original post by EasilyConfused
Well since I'm getting a 224 as the first code where KulSeran was getting a 0.

Had a wee look around cos this interested me. Have a butcher's 'ere.
Quote:the first return value will be either 0xE0 or 0x00, depending on which extended key is pressed.

So that'd be that then :o
Quote:Original post by BigFreak
Quote:Original post by EasilyConfused
Well since I'm getting a 224 as the first code where KulSeran was getting a 0.

Had a wee look around cos this interested me. Have a butcher's 'ere.
Quote:the first return value will be either 0xE0 or 0x00, depending on which extended key is pressed.

So that'd be that then :o


Well, that does explain the 224, but how come KulSeran was getting a zero as the first code for the arrow keys and I was getting the 224? Borland's compiler returns 0 as the first code for the arrow keys.

It's also still a mystery why the OP reports having to press keys four times to get my code snippet above to respond.

Thanks for the link though, BTW.
Yeah, really no idea. But wouldn't it be fair to just cover all your bases with the old if (char == 0 || char == 244) and then call getch() again? No idea why there'd be a discrepency within the same compiler though. 'sodd.
Sorry, i was typing faster than i was thinking.
I get (byte)-32 as the first code for the arrow keys/pgup pgdown
and (byte)0 as the first code for function keys

Eric, the problem with your code is that you aren't checking for 2 key presses.
void CheckForInput( void ){c = getch(); // Get Input/* Act depending on the last key received. */switch ( c ){  case -32: //  <------------------------------------------------  c = getch()  switch (c)  {    case 'M': //Right arrow key    g_cPlayer.x++;    break;          case 'K': //Left arrow key    g_cPlayer.x--;    break;        case 'H': //Up arrow key    g_cPlayer.y--;    break;         case 'P': //Down arrow key    g_cPlayer.y++;    break;        default:    break;    }  }
yeah I tried with 2 key presses but it still won't work, Here's what I changed:

void CheckForInput( void )
{
c = getch(); // Get 1st Scan
/* Act depending on the last key received. */

if (c == 0x00 || c == 0XE0) // <-----------------
{
y = getch(); // get 2nd scan
switch (y)
{
case 77: //Right arrow key
g_cPlayer.x++;
break;

case 75: //Left arrow key
g_cPlayer.x--;
break;

case 72: //Up arrow key
g_cPlayer.y--;
break;

case 80: //Down arrow key
g_cPlayer.y++;
break;

default:
break;
}
}

}
};

All that is necessary for the triumph of evil is that good men do nothing. -Edmund Burke
hey I just tried to see what value c = getch() was returning when I hit the arrow keys and nothing gets returned! I tried all the other keys and they return a number but when I press the arrow keys nothing happens... odd.

EDIT: ok I just tried it with _getch() instead and it works somewhat but now it seems to wait for a key press. Also I have to press twice for it to get the right key press. If I press left it won't show up but once I press something else it'll print out the value of left. I don't even know what the difference is between getch and _getch.

Anyway that's where I'm at.

[Edited by - Eric Poitras on November 27, 2006 4:14:09 PM]
All that is necessary for the triumph of evil is that good men do nothing. -Edmund Burke

This topic is closed to new replies.

Advertisement