how to use arrows

This is a discussion on how to use arrows within the C++ Programming forums, part of the General Programming Boards category; how i use arrows in a program? like -> to move right , <-- to move left etc......

  1. #1
    Dragoon Lover wyvern's Avatar
    Join Date
    Jul 2005
    Location
    dragooncity
    Posts
    28

    how to use arrows

    how i use arrows in a program?



    like -> to move right , <-- to move left etc...
    http://img76.imageshack.us/img76/1808/expl7pb.png

    AN EXPLOSION DOESNT HAPPEN CUZ GOD WANTS, IT HAPPENS WHEN A SOMETHING "EXPLODES

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Compiler/OS Please. On windows you would look into:

    ReadConsoleInput
    INPUT_RECORD
    Virtual-key codes

  3. #3
    Dragoon Lover wyvern's Avatar
    Join Date
    Jul 2005
    Location
    dragooncity
    Posts
    28

    ???

    ??? what? ???


    what i meaned is like:

    [code/]
    string x;

    x = getch;

    if ( x == "F" )
    // do f;

    else if ( x == "left arrow or smthing like that" )
    //move left

    [code]

    see?
    http://img76.imageshack.us/img76/1808/expl7pb.png

    AN EXPLOSION DOESNT HAPPEN CUZ GOD WANTS, IT HAPPENS WHEN A SOMETHING "EXPLODES

  4. #4
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Quote Originally Posted by wyvern
    ??? what? ???


    what i meaned is like:

    string x;

    x = getch;

    if ( x == "F" )
    // do f;

    else if ( x == "left arrow or smthing like that" )
    //move left

    see?
    Sort out your CODE tags, and AFAIK that should be:

    Code:
    if (x == 'F')  // Not "F"
    // etc...
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  5. #5
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Code:
    #include <iostream>
    #include <conio.h>
    
    int main()
    {
        int x=getch();
        while(x!=13)
        {
        std::cout<<x<<std::endl;
        x=getch();
        }
    return 0;
    }
    That displays the value of x, which is what getchar returns. It quits when getchar returns 13, which is the enter key. Figure out what the arrow keys are.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  6. #6
    Registered User
    Join Date
    Jul 2005
    Posts
    69
    Try this:
    Code:
    int a = 0;
    	
    while(a != 'q')
    {
        a = getch();
        if(a == 0 || a == 224)
            a = getch();
    
        cout << a << "\n";
    }

  7. #7
    Cat Lover
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109
    The arrow keys (for command line at least) are a combination of two codes, 224 and 75 for left, 77 right, 72 up and 80 down.

    So possibly somethething like
    Code:
    int x = 0;
    if(getch() == 224)
    {
        x = getch();
        if(x == 72){} //up
        else if(x == 75){} //left
        else if(x == 77){} //right
        else if(x == 80){} //down
    }

  8. #8
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    Processing keyboard and mouse events is covered in part 5 of my console programming tutorial.

    This is assuming you are using Windows of course, you were asked, but didn't answer.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  9. #9
    Dragoon Lover wyvern's Avatar
    Join Date
    Jul 2005
    Location
    dragooncity
    Posts
    28

    I Found Out

    I finnaly found a safe way to do it here it is


    Code:
    int x;
    
    x = getch();
    
    if ( x == 0x48 ) {}   // up
    
    else if ( x == 0x4B ) {}  // left
    
    else if ( x == 0x50){} // down
    
    else if ( x == 0x 4D) {} // right
    hope everyone finds this usefull as i also found it very usefull ^^
    http://img76.imageshack.us/img76/1808/expl7pb.png

    AN EXPLOSION DOESNT HAPPEN CUZ GOD WANTS, IT HAPPENS WHEN A SOMETHING "EXPLODES

  10. #10
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    I wrote this recently, it should basically answer your question.

    Code:
    /* ******************************************
      Name: Detecting Extended Characters in DOS 
      Author: W----------------          ** Not a chance **
      Date: 06/10/05 12:43
      Description: Shows how a programmer may
      use functions in conio.h to detect extended 
      characters while still allowing the ability 
      to read non-extended characters.             
    ****************************************** */
    
    #include <iostream.h>
    #include <conio.h>    
    
    const char UPKEY = 72;
    const char LEFTKEY = 75;
    const char RIGHTKEY = 77;
    const char DOWNKEY = 80;
    const char INSERTKEY = 82;
    const char DELETEKEY = 83;
    const char HOMEKEY = 71;
    const char ENDKEY = 79;
    const char PGUPKEY = 73;
    const char PGDNKEY = 81;
    const char SPACE = 32;
    const char ENTER = 13;
    const char TAB = 9;
    const char BSPACE = 8;
    const char ESC = 27;
    
    int main() 
    {
        bool isExtended;
        char keyPress;
        
        cout << "Press any key on your keyboard. This program should read" << endl
             << "and echo them all accordingly, with the exception of some" << endl
             << "certain keys, as they were either not defined or have" << endl 
             << "specific functions within the DOS window." << endl << endl;
        
        do
        {                  
        keyPress = getch();
        
        if (int(keyPress) == -32 || int(keyPress) == 0)  // Values returned by
        isExtended = true;                               // Extended Characters.
        else
        isExtended = false;
        
        cout << "You pressed ";
        
            if (isExtended == true)
            {           
               keyPress = getch();           // Reads in the second value of the 
                                             // Extended Character.          
               switch(keyPress)
                 {
                   case UPKEY:
                   cout << "the UP key." << endl;
                   break; 
                   case DOWNKEY:
                   cout << "the DOWN key." << endl;
                   break;
                   case LEFTKEY:
                   cout << "the LEFT key." << endl;
                   break;
                   case RIGHTKEY:
                   cout << "the RIGHT key." << endl;
                   break;
                   case INSERTKEY:
                   cout << "the INSERT key." << endl;
                   break;
                   case DELETEKEY:
                   cout << "the DELETE key." << endl;
                   break;
                   case HOMEKEY:
                   cout << "the HOME key." << endl;
                   break;
                   case ENDKEY:
                   cout << "the END key." << endl;
                   break;
                   case PGUPKEY:
                   cout << "the PAGE UP key." << endl;
                   break;
                   case PGDNKEY:
                   cout << "the PAGE DOWN key." << endl;
                   break;
                   default:
                   cout << "an undefined extended character." << endl;
                   break; 
                 }
            }
            else if (isExtended == false)
            {
               switch(keyPress)
                 {
                   case SPACE:
                   cout << "the SPACE key." << endl;
                   break;
                   case BSPACE:
                   cout << "the BACKSPACE key." << endl;
                   break;
                   case ENTER:
                   cout << "the ENTER key." << endl;
                   break;
                   case TAB:
                   cout << "the TAB key." << endl;
                   break;
                   case ESC:
                   cout << "the ESCAPE key." << endl;
                   break;
                   default:
                   cout << "the [" << keyPress << "] key." << endl;
                   break;
                 }
            }
          } while (1);
          
        return 0;
    }
    Though I'm sure there are functions in some header that already did this for you. Perhaps windows.h or something.

    Oh well, look around or do something like this.

    ...and for the hell of it, here is a program that uses the arrowkeys:

    Code:
    /* *************************************************
      Name: Active Movement Interfaces in DOS 
      Author: W---------------
      Date: 06/10/05 18:14
      Description: Demonstrates the use of the gotoxy()
      function and getch() fuction to create an active
      interface in a standard DOS program.
    ***************************************************/
    
    #include <iostream.h>
    #include <conio.h>     // Should contain a standard gotoxy() function, but
                           // mine doesn't for some reason, so I defined it 
                           // accessing Windows API directly.
    #include <windows.h>   // Needed for the gotoxy() function I used.
                        
    void gotoxy(int x, int y);                    
                        
    int main()
    {
        const char UPKEY = 72;
        const char LEFTKEY = 75;
        const char RIGHTKEY = 77;
        const char DOWNKEY = 80;
       
        int horiAXIS = 2;
        int vertAXIS = 2;
        int keyPress;
        
        cout << "Press the arrow keys to move the \"o\"." << endl << endl;
        cout << "  o";
        
        while (1) 
        {
              
              keyPress = getch();
              
              switch(keyPress)
                 {
                   case UPKEY:
                        if (vertAXIS > 1)
                          vertAXIS = vertAXIS - 1; 
                   break; 
                   case DOWNKEY:
                        vertAXIS = vertAXIS + 1;  
                   break;
                   case LEFTKEY:
                        if (horiAXIS > 0)
                          horiAXIS = horiAXIS - 1;
                   break;
                   case RIGHTKEY:
                        horiAXIS = horiAXIS + 1;
                   break;
                 }
                 
              cout << " ";                 //Clears the last character.
              gotoxy(horiAXIS,vertAXIS);
              cout << "o";                 //Creates the new character.
              gotoxy(horiAXIS,vertAXIS);   //Returns to standard space after print.
        
        } 
        
        return 0;   
    }
                       
    // I didn't write the following gotoxy() function. It can be found at:
    // http://spike.scu.edu.au/~jmaltby/c.html                    
    void gotoxy(int x, int y)
    {
    HANDLE hConsoleOutput;
    COORD dwCursorPosition;
    
    dwCursorPosition.X = x;
    dwCursorPosition.Y = y;
    hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(hConsoleOutput,dwCursorPosition);
    }
    Last edited by SlyMaelstrom; 10-12-2005 at 01:25 PM.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Directional arrows
    By Ian Paice in forum C Programming
    Replies: 4
    Last Post: 07-31-2003, 07:57 AM
  2. up, down, left, right arrows
    By revelation437 in forum C Programming
    Replies: 10
    Last Post: 12-12-2002, 08:38 AM
  3. Using Arrows in Win32 Console
    By GreenCherry in forum C++ Programming
    Replies: 2
    Last Post: 10-04-2002, 11:27 AM
  4. Arrows
    By 14n in forum C++ Programming
    Replies: 4
    Last Post: 10-02-2002, 11:30 PM
  5. arrows
    By Commander in forum C Programming
    Replies: 2
    Last Post: 02-25-2002, 06:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21