ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Thank you.
I don't know how to write such a program.
I only know hot to write a program that can be used to check the result:,
I am a beginner. Here is my simple program:
Code:
#include<iostream>
using namespace std;
int main ()
{
char c1, c2;
cout<<"press a key, maybe ESCAPE:"<<endl;
cin>>c1;
cout<<(char(27)==c1)<<endl;
cout<<"press another key, maybe TAB:"<<endl;
cin>>c2;
cout<<(char(9)==c2)<<endl;
}
When I press TAB for c2, why the program is freezed?
Can some write a program that can output the type of these keys for me?
Thank you.
Last edited by Xiangbuilder; 09-16-2003 at 06:56 AM.
the reason it froze is because of how you are reading the characters, try this(you'll need to press enter to get it to do anything because of terminal buffering)
When I run my previous program, after press tab, then press enter, the program stiill can be finished. If I press ctrl+d, it then is finished and output "0". Why?
I run your program, the character code of delete if 51, 91, 126,
why it has three ones?
by Xiangbuilder When I run my previous program, after press tab, then press enter, the program stiill can be finished. If I press ctrl+d, it then is finished and output "0". Why?
ctrl-d is the end of file marker, so when you press ctrl-d cin is marked as having no more characters, c1 is set to the null character . then the next cin is reached but cin is marked as having no more characters so the second cin does nothing.
by Xiangbuilder I run your program, the character code of delete if 51, 91, 126,
why it has three ones?
basically, using cin you are getting the ansi escape string for the key, for keys that have ascii characters like A then the ansi representation is just the ascii code but for keys that dont have an ascii representation they have longer sequences.
by Xiangbuilder Thank you.
From the program, I know most value of the keys, however, not all keys' ascii value can be print, how to know these keys'?
use my program and press the keys to get there ansi code, if you want to get the hardware codes of each key then you'll have to use a library like sdl or ncurses.
*** ***
by Xiangbuilder
When I run my previous program, after press tab, then press enter, the program stiill can be finished. If I press ctrl+d, it then is finished and output "0". Why?
ctrl-d is the end of file marker, so when you press ctrl-d cin is marked as having no more characters, c1 is set to the null character . then the next cin is reached but cin is marked as having no more characters so the second cin does nothing.
*** ***
When I run my previous program, first, I press ESCAPTE, then the program output 1, I thought, next, if I press tab, and then press enter, the program will output 1 too. However It output nothing, and I can't finish it without ctrl+d key. Why?
*** ***
by Xiangbuilder
I run your program, the character code of delete if 51, 91, 126,
why it has three ones?
basically, using cin you are getting the ansi escape string for the key, for keys that have ascii characters like A then the ansi representation is just the ascii code but for keys that dont have an ascii representation they have longer sequences.
*** ***
My English is poor, I think you mean this, if I press some keys such as DELETE that don't have ascii characters (ansii representation), the output will have longer sequences.
So, can I think so, some keys such as DELETE can't have char type?
so, what type they can have?
*** ***
by Xiangbuilder
Thank you.
From the program, I know most value of the keys, however, not all keys' ascii value can be print, how to know these keys'?
use my program and press the keys to get there ansi code, if you want to get the hardware codes of each key then you'll have to use a library like sdl or ncurses.
*** ***
Your program is good and very useful to me.
When I run it, press some keys such as CAP, CTRL, ALT, it output nothing, why?
I guess maybe this is because they don't have ansii characters (ansii representation) also, just a guess, is it right?
Another guess: the command, cout (or cin.get()) can't be used to output the ansii values of some keys such as DELETE, ALT, because they don't have ansii representation (ansii characters). So, although the program can output 51, 91, and 126 for DELET, but we can't understannd that they are the character code of DELETE, 51, 91, and 126 can be understanded as error information, just as "nothing" that "output" when I press ALT, CAP, etc.
I don't know what are the hardware codes for each key so far, I guess that maybe are difficult to me to understand, maybe I will learnd it in the future.
Sorry for my poor c++ and English. I always have many simple questions.
Last edited by Xiangbuilder; 09-16-2003 at 07:19 PM.
by Xiangbuilder I run your program, the character code of delete if 51, 91, 126,
why it has three ones?
basically, using cin you are getting the ansi escape string for the key, for keys that have ascii characters like A then the ansi representation is just the ascii code but for keys that dont have an ascii representation they have longer sequences.
True, of course, but for delete (DEL) there actually is an ascii-code: 127 (0x7F).
Do you know why this isn't used for the keyboard code?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.