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.

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..

05-08-2011

MK27

Quote:

Originally Posted by kaveh8

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):

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.