424,279 Members | 1,905 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,279 IT Pros & Developers. It's quick & easy.

Getche - Enter key

P: 6
I know that the following instructions:

char c;
c=getche();

...read a character. If I type, for example, 'g', then c=='g'. But what is equally c with if I press an arrow, the Enter key, the Del key etc.?
Sep 24 '08 #1
Share this Question
Share on Google+
10 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
Look in the ASCII table for the values of these keys.

Or, assign c to an int and display the int.
Sep 24 '08 #2

Expert 100+
P: 2,389
Look in the ASCII table for the values of these keys.
Or, assign c to an int and display the int.
Better to investigate this yourself by printing out the value of c for various keypresses. That's because:
1. ASCII encoding may be ubiquitous, but there is no law (or Standard) that requires your system to use ASCII.
2. Some keys are expressed as escape sequences, where a single keypress generates a stream of characters. To detect this, you want to loop continuously on getche, printing out whatever you get as hexadecimal text.
Sep 24 '08 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
1. ASCII encoding may be ubiquitous, but there is no law (or Standard) that requires your system to use ASCII.
2. Some keys are expressed as escape sequences, where a single keypress generates a stream of characters. To detect this, you want to loop continuously on getche, printing out whatever you get as hexadecimal text.
Yes, ASCII is not required by the standard but I have yet to see a version of C running on Unix/Linux or Windows that doesn't use it. Not to say there isn't a C lurking somewhere with a 5-bit char.

What is an example of one keypress generating a stream of characters? And I don't mean autorepeat by leaning on the key. Autorepeat is just a fast typist.
Sep 24 '08 #4

Banfa
Expert Mod 5K+
P: 8,916
Yes, ASCII is not required by the standard but I have yet to see a version of C running on Unix/Linux or Windows that doesn't use it. Not to say there isn't a C lurking somewhere with a 5-bit char.
Mobile phones and particularly text messages would be 1 recent example of a platform that may well use a character coding other than ASCII (GSM 03.38)

However I suspect that as mobile phones transform into computer like PDAs this will be less true.
Sep 24 '08 #5

Banfa
Expert Mod 5K+
P: 8,916
What is an example of one keypress generating a stream of characters? And I don't mean autorepeat by leaning on the key. Autorepeat is just a fast typist.
Any arrow key I think or any other extended keyboard keys that do not directly convert to an ASCII character.
Sep 24 '08 #6

P: 6
I wrote the following test program:

#include<iostream.h>
#include<conio.h>
void main()
{ char c;
c=getch();
cout<<endl<<c;
getche(); }

Here are some results:
Left Arrow - K
Right Arrow - M
Up Arrow - H
Down Arrow - P
Delete Key - S
At <Enter> and <Space> pushing there was no any letter in cout, maybe a space. Why was that?
PS: I want to do a short instruction like:
if (c==enter) {...}
Thanks a lot! G.P.
Sep 24 '08 #7

Expert 100+
P: 2,389
What is an example of one keypress generating a stream of characters? And I don't mean autorepeat by leaning on the key. Autorepeat is just a fast typist.
A list of the standard ANSI escape sequences can be found at
http://en.wikipedia.org/wiki/ANSI_escape_code
Sep 24 '08 #8

weaknessforcats
Expert Mod 5K+
P: 9,197
A list of the standard ANSI escape sequences can be found at
http://en.wikipedia.org/wiki/ANSI_escape_code
Thank you. I will have a look.
Sep 24 '08 #9

Banfa
Expert Mod 5K+
P: 8,916
At <Enter> and <Space> pushing there was no any letter in cout, maybe a space. Why was that?
Erm you pressed the <Space> key and you got a space ' ' would you really have wanted a letter instead?

Look carefully when you press <Enter> you get an enter (there is an extra blank line on screen).

Rather than outputting the character(s) you read to understand what is happening I suggest you output the value(s) (decimal or hexidecimal) of what you read from the keyboard.

Also see Dons link.
Sep 25 '08 #10

Expert 100+
P: 2,389
The multi-byte escape sequences can be sent to a terminal to provoke some special action. My recollection from the VT100 days was that the arrow keys on the terminal sent the corresponding cursor-move escape sequences to the computer. I don't know if today's PCs send multi-byte escape sequences or non-ASCII (>= 0x80) single characters.
Sep 25 '08 #11

Post your reply

Sign in to post your reply or Sign up for a free account.