Are they represented by numbers or words or what?
Are they represented by numbers or words or what?
key = getch();
switch(key){
case 'K': //left arrow
case 'M': //right arrow
case 'H': //up arrow
case 'P': //down arrow
}
This should work. Remember to #include stdlib.h(I think that's right) for getch().
I ment the arrow keys on a keyboard not letters!
Standard representation for GREY arrow keys:
UP: 48h (72)
LEFT: 4Bh (75)
RIGHT: 4Dh (77)
DOWN: 50h (80)
As for MSVC++, I don't know how to trap non-alphanumeric
keypresses other than the ENTER key ('\n'). One way to overcome
this would be to use inline assembly to create your own input
loop with the following instructions:
xor ax,ax ;Function #0: await a user keypress
int 16h ;Wait for a keypress: store in AX
The above keycodes would then be stored in the AH register.
They should be the same with the MS compiler, but again, I do not
know whether or not it is possible to trap them with the get()
function alone.
For more info. on inline ASM, please visit http://msdn.microsoft.com -> Search -> 'Inline Assembly'
Regards,
Peter Kimberley
[email protected]
Flikm is correct.
If you look at your ascii code charts the values kimberleyp gave equate to Flikm's statement.
UP: 48h (72) = 'H'
LEFT: 4Bh (75) = 'K'
RIGHT: 4Dh (77) = 'M'
DOWN: 50h (80) = 'P'
To make sure I wrote a program from Flikm's outline.
When reading a function key or an arrow key, _getch (no echo) and _getche (echoes) must be called twice; the first call returns 0 (function key) or 0xE0 (arrow key), and the second call returns the actual key code. Don't know why but it was documented that way in MSDN.
Code:#include "stdafx.h" #include <conio.h> #include <iostream> using namespace std; int main () { char key = _getch(); if( ( key == static_cast<char>( 0xe0 ) )|| ( key == static_cast<char>( 0x00 ) ) ) { key = _getch(); } switch(key) { case 'K': //left arrow { cout << "left arrow " << endl; break; } case 'M': //right arrow { cout << "right arrow " << endl; break; } case 'H': //up arrow { cout << "up arrow " << endl; break; } case 'P': //down arrow { cout << "down arrow " << endl; break; } default : { cout << "Not an arrow" << endl; break; } } return 0; }
Thanks!