Thread: how do you represent the arrow keys in c++

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    61

    how do you represent the arrow keys in c++

    Are they represented by numbers or words or what?

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    305
    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().

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    61
    I ment the arrow keys on a keyboard not letters!

  4. #4
    kimberleyp
    Guest

    Arrow

    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]

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    156
    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; 
    }

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    61
    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Movement with arrow keys
    By louis_mine in forum C Programming
    Replies: 3
    Last Post: 02-06-2005, 04:35 PM
  2. Interfacing with arrow keys...
    By adityakarnad in forum Game Programming
    Replies: 1
    Last Post: 08-30-2003, 10:25 PM
  3. Ascii code for arrow keys
    By beginner in forum C Programming
    Replies: 1
    Last Post: 11-07-2002, 01:29 PM
  4. msdos arrow keys?
    By seditee in forum Game Programming
    Replies: 3
    Last Post: 05-07-2002, 11:29 PM
  5. Arrow keys
    By Nutshell in forum C Programming
    Replies: 5
    Last Post: 03-27-2002, 11:49 AM