Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Esc key in C program

Esc key in C program
Thread Tools
Junior Member
Join Date: Mar 2000
Location: Salem, OR, USA
Status: Offline
Reply With Quote
Mar 11, 2001, 03:19 AM
 
Right off the bat let me say this is for an assignment. The bulk of the assignment I think I have figured out. It is just this one little thing. . .

We have to read a number from the keyboard. If the user got to this point by mistake they are to hit "Esc" to exit, otherwise they will enter the ID number of the student they want to edit.

I have tried reading a variable of type char and then testing if it is == 27 and I have tried getchar(). In everything I tried I have to hit 'enter' for it to go on. How do I get it so that if I hit just ESC (without enter) the character will be read in?

My text book is of no help. If I only knew the name of the function (if there is one) I could look it up in my reference book and go from there.

Thanks to anyone who can help.
     
Dedicated MacNNer
Join Date: Nov 1999
Location: Georgetown, Demerara, Guyana
Status: Offline
Reply With Quote
Mar 12, 2001, 01:25 AM
 
Hi numero,

I assume you're using MetroWerks' CodeWarrior IDE? If so, there's a special 'getch()' function that allows "immediate" character reads in a Mac SIOUX (console) app. [You'd need to '#include <console.h>' to pull in the 'getch()' prototype.] Note that there is one quirk involved in calling 'getch()' -- viz., it doesn't echo the user's typed char(s) back to the screen. So, to provide feedback when the user types a non-ESC char (i.e., the first char of a student-ID string), you'd need to explicitly echo that char by calling an output function such as 'putchar()'.

If the user has indeed typed a non-ESC char, you could also "undo" the 'getch()' read, thus restoring that char to the 'stdin' input stream, by calling 'ungetc( char, stdin )'. You could then just call an input function like 'gets()' to read (& auto-echo) the entire ID string as usual. [Or, alternatively you could continue making calls to 'getch()' & 'putchar()' in a loop to read & echo the remaining ID chars (exiting when you get a certain keystroke such as the '\r' Return char). Within the loop, you'd also need to call something like 'strcat()' to "accumulate" each successive char into a complete ID string. One potential advantage of this latter approach is that it would allow the user to press the ESC key at any point during the typing of the ID string.]

There are probably many better ways to accomplish all of this, but the above approaches would work okay as first stabs at a solution. Of course, the 'getch()' function isn't a standard ANSI routine, so it might not be acceptable for your assignment. Unfortunately, the "buffering" of the 'stdin' input stream cannot normally be disabled via the 'setbuf()' function, nor can it be bypassed by using other input functions like 'scanf()'... I don't remember if Apple's own MPW IDE automatically implements an "immediate" mode for the standard 'getchar()' function?

Regards,

--Paul

[This message has been edited by Paul Crawford (edited 03-12-2001).]
     
numero  (op)
Junior Member
Join Date: Mar 2000
Location: Salem, OR, USA
Status: Offline
Reply With Quote
Mar 16, 2001, 01:04 AM
 
Paul,

Thanks for your reply. That helps a lot.

-numero
     
   
Thread Tools
Forum Links
Forum 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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 11:03 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2