Like Tree1Likes
  • 1 Post By kaveh8

Read Arrow Keys in C++

This is a discussion on Read Arrow Keys in C++ within the C++ Programming forums, part of the General Programming Boards category; Hi guys. I would like to write a program in C++ ,doing something after pressing arrow keys.at first i decided ...

  1. #1
    Registered User kaveh8's Avatar
    Join Date
    May 2011
    Location
    Iran , Gorgan
    Posts
    2

    Read Arrow Keys in C++

    Hi guys.
    I would like to write a program in C++ ,doing something after pressing arrow keys.at first i decided to test reading arrow keys.i wrote a simple program that after pressing arrow keys prints in console screen which one of arrow keys is pressed.for writing this prog i require to find out Ascii values of this 4 keys.therefore i wrote a program print Ascii value of a keyboard key when i press that key.after running this program i find out this value is 3 byte.in my OS (Ubuntu 10.10) this 3 value is 27 , 91 , and 3rd part for UP key ,DOWN key ,RIGHT key ,LEFT key is consecutively 65,66,67 and 68.i expect this prog runs at Windows OS successfully ,because i expect this values be the same of Ubuntu in Windows OS.in Windows this prog is unusable and after discovering Ascii codes of my arrow keys in Windows (that is 2 byte 0x0H) i wrote another special prog for Windows.
    I would like to write a portable program ,which can use in different type of OS?what's the solution?why ascii values are different in operating systems?
    At the following you can see this progs ,in Ubuntu and also in Windows.

    Sincerely.
    Kaveh Shahhosseini 1/May/2011

    Code:
    //========================
    //this progs wrote in C programming language.
    //Ubuntu...used only 65,66,67,68 values instead of "27,91,65" , "27,91,66" ...
    #include <stdio.h>
    //--------------------------
    int main()
    {
        char ch;
        do{
            ch=getchar();
             if(ch==65)
                printf("You pressed UP key\n");
             else if(ch==66)
                printf("You pressed DOWN key\n");
             else if(ch==67)
                printf("You pressed RIGHT key\n");
             else if(ch==68)
                printf("You pressed LEFT key\n");
    
    
    
        }while(ch!='e');
     return 0;
    }
    Code:
    //======================
    //Windows...
    #include <stdio.h>
    #include <conio.h>
    using namespace::std;
    //-----------------------------
    int main()
    {
          char ch;
      do{
    
            ch=getch();
            if(ch!='\0')
            {
              ch=getch();
              if(ch=='H')
                  printf("UP\n");
                     else    if(ch=='P')
                  printf("DOWN\n");
                       else    if(ch=='M')
                  printf("RIGHT\n");
                         else   if(ch=='K')
                  printf("LEFT\n");
            }
      }while(ch!='e');
    
        return 0;
    }
    //====================
    Samanelec likes this.

  2. #2
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    Quote Originally Posted by kaveh8 View Post
    I would like to write a portable program ,which can use in different type of OS?what's the solution?why ascii values are different in operating systems?
    Sorry, you can't. What you want to do is not native to C++ so the way to do this would have to be specially added to your compiler. And most compilers don't have these options. And those that do (in general) do it their own way since there is no standard for it..
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by kaveh8 View Post
    why ascii values are different in operating systems?
    Because those are not ascii values. ASCII stands for American Standard Code for Information Interchange, it is a standard assigning the 128 possible 7-bit values to characters, including some now mostly obsolete "non-printing" characters. C/C++ uses ascii natively.

    What you are dealing with coming from your keyboard are 7 or 8 bit integer values, that does not mean they have anything to do with ASCII. My street address is number 43, but that does not make it meaningful for me to say I live at +, because ASCII is not used for street addresses.

    So, it is not that Windows and Ubuntu use some different form of ascii to represent the extended keyboard. They do not use ascii at all, because the extended keyboard is not part of ascii. The ASCII standard is complete, and has no room for them: there are only 128 possible 7-bit numbers, and ASCII uses singular 7-bit values.

    But as you observe, the sequences of numbers used to represent the extended keyboard do follow a pattern. Linux uses a standard called ANSI escape sequences (ANSI stands for the American National Standards Institute):

    https://secure.wikimedia.org/wikiped...SI_escape_code

    If you skip down to "Windows and DOS" in that article, you can find out what happened there. Basically, there is no portable standard because Microsoft decided to give up ANSI. C/C++ does not natively support it either. ASCII was already finalized before C was developed, ANSI was not; As WaltP says, C/C++ has no standard to represent the extended keyboard.
    Last edited by MK27; 05-08-2011 at 02:00 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Would ncurses be able to do this?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to read arrow keys in Linux?
    By edugarcia in forum C Programming
    Replies: 4
    Last Post: 11-09-2004, 02:20 PM
  2. Using the arrow keys
    By Punkture in forum C Programming
    Replies: 8
    Last Post: 05-31-2003, 04:25 PM
  3. arrow keys, ctrl and alt keys
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-25-2002, 03:53 PM
  4. Arrow keys
    By Nutshell in forum C Programming
    Replies: 5
    Last Post: 03-27-2002, 10:49 AM
  5. Arrow Keys and Such
    By Thantos in forum Game Programming
    Replies: 5
    Last Post: 10-25-2001, 05:40 PM

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