LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-16-2003, 02:18 AM   #1
Xiangbuilder
Member
 
Registered: Apr 2003
Location: Shandong province China
Distribution: fedora core 1.0
Posts: 206

Rep: Reputation: 30
What type many keys are?


I think the number keys is int type, the word keys is char in c++.
What about other keys? I only know ESCAPE key is char(27) so far.
Thank you.

Last edited by Xiangbuilder; 09-16-2003 at 02:46 AM.
 
Old 09-16-2003, 04:14 AM   #2
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
tab is 9 i think. why dont you write a program to find out.
 
Old 09-16-2003, 06:51 AM   #3
Xiangbuilder
Member
 
Registered: Apr 2003
Location: Shandong province China
Distribution: fedora core 1.0
Posts: 206

Original Poster
Rep: Reputation: 30
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.
 
Old 09-16-2003, 07:04 AM   #4
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
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)
Code:
#include <iostream>

using namespace std;

int main()
{
    while(!cin.eof()) 
        cout << "character code " << cin.get() << endl;
    return 0;
}
 
Old 09-16-2003, 09:40 AM   #5
Xiangbuilder
Member
 
Registered: Apr 2003
Location: Shandong province China
Distribution: fedora core 1.0
Posts: 206

Original Poster
Rep: Reputation: 30
Thank you.

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?
 
Old 09-16-2003, 09:58 AM   #6
shobhit
Member
 
Registered: Sep 2003
Location: kolkata
Distribution: Fedora 7
Posts: 60

Rep: Reputation: 15
try running this. it will print the ascii values and their corresponding characters.

void main()
{
int i;
for(i=0;i<=255;i++)
{
if(i!=26)//the program hangs for 26
cout<<"\n"<<i<<"\t"<<char(i);
}
}
 
Old 09-16-2003, 10:27 AM   #7
Xiangbuilder
Member
 
Registered: Apr 2003
Location: Shandong province China
Distribution: fedora core 1.0
Posts: 206

Original Poster
Rep: Reputation: 30
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'?
 
Old 09-16-2003, 11:30 AM   #8
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
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.
 
Old 09-16-2003, 07:16 PM   #9
Xiangbuilder
Member
 
Registered: Apr 2003
Location: Shandong province China
Distribution: fedora core 1.0
Posts: 206

Original Poster
Rep: Reputation: 30
Thank you.

*** ***
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.
 
Old 09-16-2003, 09:29 PM   #10
shellcode
Member
 
Registered: May 2003
Location: Beverly Hills
Distribution: Slackware, Gentoo
Posts: 350

Rep: Reputation: 32
$man ascii
 
Old 09-17-2003, 01:14 AM   #11
Xiangbuilder
Member
 
Registered: Apr 2003
Location: Shandong province China
Distribution: fedora core 1.0
Posts: 206

Original Poster
Rep: Reputation: 30
Thank you.
 
Old 09-17-2003, 12:35 PM   #12
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally posted by kev82

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?

Last edited by Hko; 09-17-2003 at 12:38 PM.
 
Old 09-17-2003, 02:05 PM   #13
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
by Hko
Do you know why this isn't used for the keyboard code?

i dont know, i would take a guess that it comes from a 'feature' of some ancient terminal that nobody has used in 35 years
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
installing redhat 8.0 when text login in I cannot type password?? I type root scrist Linux - General 4 04-22-2006 02:20 AM
arrays of elements with [gcc4]array type has incomplete element type lmmix Linux - Software 0 02-26-2005 08:07 AM
with keys is the keys that change languages? AKAKAK Fedora 2 01-25-2005 10:11 AM
root (hd 0,0)Filesystem type unknown, partition type 0x7chainloader +1 ece30675 Linux - Distributions 5 07-20-2004 09:04 AM
Configuring SSH to accept only keys (already have keys) fr0st Linux - Security 3 11-04-2003 03:31 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration