Movement with arrow keys

This is a discussion on Movement with arrow keys within the C Programming forums, part of the General Programming Boards category; Hi I have been trying to create a program which movement is using the arrow keys, however i havenīt been ...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    66

    Movement with arrow keys

    Hi I have been trying to create a program which movement is using the arrow keys, however i havenīt been succesful, I donīt know why this is not recognizing the arrow keys, i have even copied and tried to run the example in the FAQ, however, the only key it recognizes is the ESC key, it won't print anithing if I press the arrow keys, i would like to know if someone could help me to correct this because i have used another example program of how to use getch() and it doesnīt recognize the keys, it is supposed to print something for each key I press but for the arrows it will print nothing.

    this is the code in the FAQ i have used:
    [CODE]enum
    {
    KEY_ESC = 27,
    ARROW_UP = 256 + 72,
    ARROW_DOWN = 256 + 80,
    ARROW_LEFT = 256 + 75,
    ARROW_RIGHT = 256 + 77
    };

    static int get_code ( void )
    {
    int ch = getch();

    if ( ch == 0 || ch == 224 )
    ch = 256 + getch();

    return ch;
    }

    int main ( void )
    {
    int ch;

    while ( ( ch = get_code() ) != KEY_ESC ) {
    switch ( ch ) {
    case ARROW_UP:
    printf ( "UP\n" );
    break;
    case ARROW_DOWN:
    printf ( "DOWN\n" );
    break;
    case ARROW_LEFT:
    printf ( "LEFT\n" );
    break;
    case ARROW_RIGHT:
    printf ( "RIGHT\n" );
    break;
    }
    }

    return 0;
    }
    [/CODE)


    And this is the example program of the C help on getch()

    Code:
                 /* getch example */
    
    #include <conio.h>
    #include <stdio.h>
    
    int main(void)
    {
      int c;
      int extended = 0;
      c = getch();
      if (!c)
         extended = getch();
      if (extended)
         printf("The character is extended\n");
      else
         printf("The character isn't extended\n");
    
      return 0;
    }
    I hope spmeone can help me, i have been trying to do this for three days.
    Thx


    My OS is win XP home

    My Compiler is borland, I use Turbo C++ version 4.5
    Last edited by louis_mine; 02-06-2005 at 03:44 PM.

  2. #2
    Registered User Sake's Avatar
    Join Date
    Jan 2005
    Posts
    89
    Does get_code return the same values wen you test it as in the enum?
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    static int get_code ( void )
    {
      int ch = getch();
    
      if ( ch == 0 || ch == 224 )
        ch = 256 + getch();
    
      return ch;
    }
    
    int main(void)
    {
      int x;
    
      while ((x = get_code()) != 0x1B)
        printf("%d\n", x);
    
      return 0;
    }
    The actual scan codes may differ from system to system.
    Kampai!

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    66
    That code returns a numerical value for each key I press, different for each letter, it doesnīt print anything if I use the arrow keys ;(

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    There is much about this which is OS and compiler specific, so details please.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Interfacing with arrow keys...
    By adityakarnad in forum Game Programming
    Replies: 1
    Last Post: 08-30-2003, 10:25 PM
  2. Ascii code for arrow keys
    By beginner in forum C Programming
    Replies: 1
    Last Post: 11-07-2002, 12:29 PM
  3. Arrow keys!
    By Paninaro in forum C Programming
    Replies: 8
    Last Post: 06-26-2002, 07:39 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, 10:49 AM

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