473,545 Members | 2,043 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How does ESC key interact with ANSI C programs?

hugo27 July 13, 2004

The teachy books and documentation I've read do not
mention the Escape key, but everytime I hit it during
runtime my programs go bananas.

This relates to a larger issue, namely, what is
standard C good for? Of what use is it?
We have the null set: each person writes and
uses their own progams. This works to a degree, but
it makes for a small world. What happens when you
venture into the larger world and meet up with
Other People (aka, The Damm Thing)?
Suppose one writes a common menu program for an
employer and someone sonetime presses ESC, the
program crashes, and the client wants their money
back? Are you going to tell the judge that the
complaint is Off Topic?

Is there a way in ANSI C to disable or manage
the Escape key? I've tried capturing ascii 27
with scanf and getchar, but it drives the program
batty.

Thanks for your thoughts.
hugo

Nov 14 '05 #1
17 9197
[newbie answer]
hugo27 wrote:
Is there a way in ANSI C to disable or manage
the Escape key?


I guess there isn't.

A microwave oven could have programs written in ANSI C.
A mobile phone could have programs written in ANSI C.
A electronic stopwatch could have programs written in ANSI C.
....

Where is the Escape key in all these things? :-)

--
USENET would be a better place if everybody read: | to email me: use |
http://www.catb.org/~esr/faqs/smart-questions.html | my name in "To:" |
http://www.netmeister.org/news/learn2quote2.html | header, textonly |
http://www.expita.com/nomime.html | no attachments. |
Nov 14 '05 #2

"hugo27" <ob****@yahoo.c om> wrote in message
news:a7******** *************** ***@posting.goo gle.com...
hugo27 July 13, 2004

The teachy books and documentation I've read do not
mention the Escape key, but everytime I hit it during
runtime my programs go bananas.

This relates to a larger issue, namely, what is
standard C good for? Of what use is it?
We have the null set: each person writes and
uses their own progams. This works to a degree, but
it makes for a small world. What happens when you
venture into the larger world and meet up with
Other People (aka, The Damm Thing)?
Suppose one writes a common menu program for an
employer and someone sonetime presses ESC, the
program crashes, and the client wants their money
back? Are you going to tell the judge that the
complaint is Off Topic?

Is there a way in ANSI C to disable or manage
the Escape key?
No, because ANSI (formally "ISO") C does not define
such a thing as a 'key', a 'keyboard', or any hardware
devices at all. All i/o is abstracted as 'streams of
characters'.
I've tried capturing ascii 27


The ASCII character set does define a value 27 with
the mnemonic 'ESC', but that doesn't necessarily
correspond to what a PC keyboard's 'escape' key
generates.

The C language does not require using the ASCII character
set. It allows any character set which contains the mimimum
set of characters required by the standard. The actual encodings
of these characters is left to the implementation. None of them
is called 'escape'.
with scanf and getchar, but it drives the program
batty.


On a typical PC, neither 'scanf()' nor 'getchar()' will 'see'
any input from the 'ESC' key. It doesn't generate a 'character'
from the perspective of C's i/o library.

You'll need to use a library 'extension' (if your implementation
provides one), or perhaps a third party library.

What you're trying to do involves direct communication with
your operating system and/or hardware, neither of which is
supported by ISO C.

E.g. if you're working with a Windows system, try www.msdn.microsoft.com

-Mike
Nov 14 '05 #3

"Pedro Graca" <he****@hotpop. com> wrote in message
news:sl******** ***********@ID-203069.user.uni-berlin.de...
[newbie answer]
hugo27 wrote:
Is there a way in ANSI C to disable or manage
the Escape key?


I guess there isn't.

A microwave oven could have programs written in ANSI C.
A mobile phone could have programs written in ANSI C.
A electronic stopwatch could have programs written in ANSI C.
...

Where is the Escape key in all these things? :-)


Prison security control systems could be written in C.
The hardware probably won't have an escape key. :-)

-Mike

Nov 14 '05 #4
In article <sl************ *******@ID-203069.user.uni-berlin.de>,
Pedro Graca <he****@hotpop. com> wrote:
[newbie answer]
hugo27 wrote:
Is there a way in ANSI C to disable or manage
the Escape key?


I guess there isn't.

A microwave oven could have programs written in ANSI C.
A mobile phone could have programs written in ANSI C.
A electronic stopwatch could have programs written in ANSI C.
...

Where is the Escape key in all these things? :-)


Why, in the same place where the "&" key is.

--
rr
Nov 14 '05 #5
ob****@yahoo.co m (hugo27) writes:
hugo27 July 13, 2004

The teachy books and documentation I've read do not
mention the Escape key, but everytime I hit it during
runtime my programs go bananas.
We have no way of knowing why that happens without more details. The
C standard says nothing about the escape key. On any system I've
used, I wouldn't expect hitting escape while a program is running to
do anything more drastic than hitting the space bar, unless the
program itself (or some routine it calls) specifically handles it.

It would also be useful to know what you mean by "go bananas".

You don't mention what operating system you're using; since your
problem is probably system-specific, that could be a vital piece of
information.

You mention a "menu program", which leads me to guess that you're
using the curses library on a Unix-like system. If so, you may be
having problems with escape sequences. Arrow keys, for example,
typically send sequences starting with <escape>, and your program
could be seeing the <escape> and waiting for the rest of the sequence.

This is not an ISO C (or ANSI C, if you prefer) issue. If my guess is
correct, you should consult your documentation; if that doesn't work,
try searching the web; if that doesn't work, ask in comp.unix.progr ammer,
but be sure to provide more details than you've given us here. (If my
guess isn't correct, replace comp.unix.progr ammer with some newsgroup
appropriate to your system.)
This relates to a larger issue, namely, what is
standard C good for? Of what use is it?


No, it really doesn't.

One more thing: when you post to Usenet, *please* don't start each
line you write with '>'. The '>' characters you see in Usenet
postings aren't just decorative; they indicate text that you've quoted
from somebody else's article. If every line of your article starts
with '>', it looks like you've replied to someone else without adding
anything of your own, and you're likely to be ignored.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #6
hugo27 wrote:
The teachy books and documentation [that] I've read
do not mention the Escape key
but, everytime I hit it during runtime,
my programs go bananas. This relates to a larger issue, namely,
what is standard C good for? Of what use is it?
We have the null set: each person writes and
uses their own progams. This works to a degree,
but it makes for a small world.
What happens when you venture into the larger world
and meet up with Other People (aka, The Damm Thing)?
Suppose one writes a common menu program for an
employer and someone sonetime presses ESC,
the program crashes,
and the client wants their money back?
Are you going to tell the judge that
the complaint is Off Topic? Is there a way in ANSI C
to disable or manage the Escape key?
I've tried capturing ascii 27 with scanf and getchar,
but it drives the program batty.
cat main.c #include <stdio.h>
#include <stdlib.h>

int main(int argc, char* argv[]) {
int c = '\0';
while (EOF != (c = getchar()))
fprintf(stdout, "%d\n", c);
return EXIT_SUCCESS;
}
gcc -Wall -std=c99 -pedantic -o main main.c
./main 13
49
51
10
^[
27
10 gcc --version

gcc (GCC) 3.3.3 20040412 (Red Hat Linux 3.3.3-7)

I typed ^D to input End Of File (EOF) to terminate the program.
It seems to work fine for me.

This is the comp.lang.c newsgroup and not the
everything.you. need.to.know.ab out.computer.pr ogramming newsgroup.
You will need to consult other resources
if you need to write platform dependent device drivers.
We don't know everything -- we're just know-it-alls --
but, if you tell us which platform is a target for your C program,
we may be able to redirect your question to a more appropriate forum
where you can get reliable expert advice.

platform = computer architecture + operating system + C compiler

Nov 14 '05 #7
>>The teachy books and documentation I've read do not
mention the Escape key, but everytime I hit it during
runtime my programs go bananas.
What brand of bananas?
You need to be more explicit what happens. I have some problems
with the computer rebooting every time I press the RESET button,
and it seems to go completely dead if I press the POWER OFF button.
Bug reports on this topic generally go unanswered for some reason.

If your platform even HAS an escape key, it's just a character.
A sequence beginning with the escape character (including one
that is echoed) might cause your terminal to do something special.

Some technical terms:

"doesn't work" means that at least 4 members of an AFL-CIO union
are picketing outside your house or place of residence carrying
signs that have at least the word "UNFAIR" on them.

"crashes" means that a reboot is required to get the operating
system working again. It is unusual for an operating system
with memory protection to have problems with a user-level
program crashing it, but some vendors manage to have bugs like this.
Suppose one writes a common menu program for an
employer and someone sonetime presses ESC, the
program crashes, and the client wants their money
back?
What platform does this happen on? Your program should be
able to tell the user that escape isn't a valid choice, and
repeat the menu.

It is unnecessary to trap() characters, with or without bait().
Neither of those functions are supported by ANSI C or much
of anything else.
Is there a way in ANSI C to disable or manage
the Escape key? I've tried capturing ascii 27
with scanf and getchar, but it drives the program
batty.


How did you do this? Most platforms don't support capture(),
trap(), or bait().

"batty": the program runs around in a cape and mask searching
for The Penguin, but ends up capturing Linus instead.

Gordon L. Burditt
Nov 14 '05 #8
In <a7************ **************@ posting.google. com> ob****@yahoo.co m (hugo27) writes:
The teachy books and documentation I've read do not
mention the Escape key, but everytime I hit it during
runtime my programs go bananas.
Mine don't. They have no reason to.
This relates to a larger issue, namely, what is
standard C good for? Of what use is it?
Writing code that works everywhere. Or, more realistically, writing
code whose non-portable sections can be isolated from the portable
sections, so only the former need to be touched in the porting process.
The smaller they are, the easier to port the code.
We have the null set: each person writes and
uses their own progams. This works to a degree, but
it makes for a small world. What happens when you
venture into the larger world and meet up with
Other People (aka, The Damm Thing)?
Nothing special.
Suppose one writes a common menu program for an
employer and someone sonetime presses ESC, the
program crashes, and the client wants their money
back? Are you going to tell the judge that the
complaint is Off Topic?
No, but I have yet to write a program that crashes when the user presses
the ESC key. Can you show me some sample code, so that I get a clue about
how to do it (other than by *explicitly* testing for the ESC character and
deliberately crashing the program when finding it in the user input, of
course)?
Is there a way in ANSI C to disable or manage
the Escape key? I've tried capturing ascii 27
with scanf and getchar, but it drives the program
batty.


It should be trivial to detect the ESC character in user input, but
why bother? It's much easier to only focus on correct user input and
reject everything else as bad input.

For much better advice, post some code causing problems when the ESC
key is pressed and accurately describe the nature of those problems.

If I accidentally press the ESC key while providing input to my programs,
the tty driver displays ^[, the convetional printable representation of
the ASCII ESC character. I can press the backspace key to remove it,
just as I can press the backspace key to remove any typo.

Here's a sample/simple program:

fangorn:~/tmp 426> cat test.c
#include <stdio.h>
#include <string.h>

#define ESC '\33'
char buf[BUFSIZ];

int main()
{
if (fgets(buf, sizeof buf, stdin) == NULL) return 0;
if (strchr(buf, ESC) == NULL) printf("No ");
puts("ESC detected in user input.");
return 0;
}
fangorn:~/tmp 427> gcc test.c
fangorn:~/tmp 428> ./a.out
qwertyuioighj
No ESC detected in user input.
fangorn:~/tmp 429> ./a.out
qwer^[erty
ESC detected in user input.

Yeah, for toy programs, even fgets is good enough ;-)

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #9
Dan Pop wrote:
No, but I have yet to write a program that crashes when the user presses
the ESC key. Can you show me some sample code, so that I get a clue about
how to do it (other than by *explicitly* testing for the ESC character and
deliberately crashing the program when finding it in the user input, of
course)?

I tried what he said with my text adventure game, running under windows,
and it does crash when the input from the user is <ESC> <ENTER>. The
program reports back that [some long line of screen-draw characters] is
not a valid verb, then the system itself pops a dialog: The instruction
at "0x6f36572" referenced memory at "0x6f36572" . The memory could not be
"read".

I'll have to run through in debug and see if I can figure out why it's
doing that. Probably a bug on my end. Doesn't happen under Solaris.


Brian Rodenborn
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

38
3286
by: Martin Marcher | last post by:
Hi, I've read several questions and often the answer was 'C knows nothing about .' So if C knows that little as some people say, what are the benefits, I mean do other languages know more or is it a benefit that C knows nearly nothing (what I can think about is that C is the largest common divisor defined on most available platforms)?
100
6837
by: Roose | last post by:
Just to make a tangential point here, in case anyone new to C doesn't understand what all these flame wars are about. Shorthand title: "My boss would fire me if I wrote 100% ANSI C code" We are discussing whether this newsgroup should focus on 100% ANSI C or simply topics related to the C language in the real world. There is a C...
22
3044
by: Ryan M | last post by:
I've been programming for a while, but most of my experience is on unix. How do C compilers work on operating systems that weren't written in C? And that have no libc? Compiling C on unix seems so easy. Everything in the code either goes right to machine code, or links to a C library (often libc) or links to the kernel. Are there libc...
24
3772
by: David Mathog | last post by:
If this: int i,sum; int *array; for(sum=0, i=0; i<len; i++){ sum += array; } is converted to this (never mind why for the moment):
74
3956
by: Suyog_Linux | last post by:
I wish to know how the free()function knows how much memory to be freed as we only give pointer to allocated memory as an argument to free(). Does system use an internal variable to store allocated memory when we use malloc(). Plz help......
5
7531
by: S.Tobias | last post by:
Since I haven't found any recent reference to it in csc or clc, I would like to bring Readers' attention to the apparent availability of C90 Standard in electronic form from www.ansi.org. I haven't actually bought it, and I have rather assumed it's a PDF file (but too many things indicate it is). (I hope I'm not discovering something...
83
11538
by: sunny | last post by:
Hi All What is C99 Standard is all about. is it portable, i mean i saw -std=C99 option in GCC but there is no such thing in VC++.? which one is better ANSI C / C99? can i know the major difference between C99 & ANSI C standards?
40
3592
by: Spiros Bousbouras | last post by:
Do you have an example of an implementation where sizeof(short int) does not divide sizeof(int) or sizeof(int) does not divide sizeof(long int) or sizeof(long int) does not divide sizeof(long long int) ? Same question for the corresponding unsigned types.
8
2056
AmberJain
by: AmberJain | last post by:
HELLO, Is it necessary for a C programmer to have an ANSI C standard or it's sufficient to own Kernigham and Rithie's The C programming language? I know that the ritchie's book is quite brief and precise. But still, do I need a ANSI C standard? Also, Which ANSI C standard should I prefer i.e. ANSI C89 or ANSI C99 to implement in my C...
0
7468
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7401
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7656
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7423
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7757
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5329
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3450
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3443
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
704
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.