Welcome to MacForumz.com!
FAQFAQ      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

non-line-buffered key input in C

 
   Macintosh computer (Home) -> Programmer Help RSS
Next:  non-line-buffered key input in C?  
Author Message
Eric Pellegrin

External


Since: May 24, 2005
Posts: 3



(Msg. 1) Posted: Tue May 24, 2005 2:55 am
Post subject: non-line-buffered key input in C
Archived from groups: comp>sys>mac>programmer>help (more info?)

I'm learning C from a book that uses the getche() function from the
<conio.h> library. However, the preprocessor/compiler (gcc 3.3 on
Panther) doesn't recognize the function nor the library. The getchar()
function only works line-buffered.

How do I get a program to accept character input from the keyboard
without waiting for the user to press Return?

Any help would be much-appreciated.

Eric

 >> Stay informed about: non-line-buffered key input in C 
Back to top
Login to vote
David Phillip Oste

External


Since: Apr 25, 2004
Posts: 1083



(Msg. 2) Posted: Tue May 24, 2005 4:55 am
Post subject: Re: non-line-buffered key input in C [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

In article <uhzke.134983$AE6.11272@tornado.texas.rr.com>,
Eric Pellegrin <epellegrin2.no.spam.please.DeleteThis@sw.rr.com> wrote:

 > I'm learning C from a book that uses the getche() function from the
 > <conio.h> library. However, the preprocessor/compiler (gcc 3.3 on
 > Panther) doesn't recognize the function nor the library. The getchar()
 > function only works line-buffered.
 >
 > How do I get a program to accept character input from the keyboard
 > without waiting for the user to press Return?

You can use Unix calls to put the standard input into an unbuffered
state, or you can use Carbon calls to poll for events with WaitNextEvent
or use Carbon's GetKeys() to read the state of the keyboard, or you can
use CarbonEvent calls to register a callback that will be called on each
keyDown, or you can use Cocoa, and override the -(void)keyDown: handler,
which by default calls the -interpretKeyEvent: handler, or you can use
OpenGL's Glut to have keyDowns delivered to your callback, you can do an
optional install of X11 and use X11 callbacks to get at the keyboard, or
you can reference the keyboard directly using IOKit.

That's 8 ways to do it. If you are just learning C, you should just type
the carriage return, and modify your code to ignore the extra character,
until you know more.

--
David Phillip Oster<!-- ~MESSAGE_AFTER~ -->

 >> Stay informed about: non-line-buffered key input in C 
Back to top
Login to vote
Reinder Verlinde

External


Since: Feb 18, 2004
Posts: 173



(Msg. 3) Posted: Tue May 24, 2005 2:55 pm
Post subject: Re: non-line-buffered key input in C [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

In article <uhzke.134983$AE6.11272@tornado.texas.rr.com>,
Eric Pellegrin <epellegrin2.no.spam.please.TakeThisOut@sw.rr.com> wrote:

 > I'm learning C from a book that uses the getche() function from the
 > <conio.h> library. However, the preprocessor/compiler (gcc 3.3 on
 > Panther) doesn't recognize the function nor the library. The getchar()
 > function only works line-buffered.
 >
 > How do I get a program to accept character input from the keyboard
 > without waiting for the user to press Return?

This is not a C question. See question 19.1 in the C FAQ
<http://www.faqs.org/faqs/C-faq/faq/>:

 > Q: How can I read a single character from the keyboard without
 > waiting for the RETURN key? How can I stop characters from
 > being echoed on the screen as they're typed?
 >
 > A: Alas, there is no standard or portable way to do these things in
 > C. Concepts such as screens and keyboards are not even
 > mentioned in the Standard, which deals only with simple I/O
 > "streams" of characters.

You will have to use platform-specific stuff. Windows supports this in
its libraries, using <conio.h>.

On Mac OS X, you will have to do this by setting your terminal
characteristics, using stty. 'man stty' lists all options. Something
like:

stty raw
stty -ignbrk brkint isig
./my_executable

will probably get your terminal into the state you need.

Reinder<!-- ~MESSAGE_AFTER~ -->
 >> Stay informed about: non-line-buffered key input in C 
Back to top
Login to vote
Eric Pellegrin

External


Since: May 24, 2005
Posts: 3



(Msg. 4) Posted: Wed May 25, 2005 2:55 am
Post subject: Re: non-line-buffered key input in C [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

  >>How do I get a program to accept character input from the keyboard
  >>without waiting for the user to press Return?
 >
 >
 > This is not a C question. See question 19.1 in the C FAQ
 > <http://www.faqs.org/faqs/C-faq/faq/>:

Hey. Thanks for the link. Much interesting stuff there, and unusual
(20.35 "Duff's Device"). To return the favor, have you checked out C
creator Dennis Ritchie's home page?
<http://www.cs.bell-labs.com/who/dmr/index.html>:

There's some very interesting history and anecdotes about C and UNIX
from the old days when Ritchie and Ken Thompson were founding the
language and the system at Bell Labs.

 > You will have to use platform-specific stuff. Windows supports this in
 > its libraries, using <conio.h>.
 >
 > On Mac OS X, you will have to do this by setting your terminal
 > characteristics, using stty. 'man stty' lists all options. Something
 > like:
 >
 > stty raw
 > stty -ignbrk brkint isig
 > ./my_executable
 >
 > will probably get your terminal into the state you need.

This is exactly the kind of help I was looking for. It worked fine. Now
I'll have to go through the stty man page to learn how to disable this
when I'm done.

Thanks again.

Eric<!-- ~MESSAGE_AFTER~ -->
 >> Stay informed about: non-line-buffered key input in C 
Back to top
Login to vote
Eric Pellegrin

External


Since: May 24, 2005
Posts: 3



(Msg. 5) Posted: Wed May 25, 2005 2:55 am
Post subject: Re: non-line-buffered key input in C [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

  >>How do I get a program to accept character input from the keyboard
  >>without waiting for the user to press Return?
 >
 >
 > You can use Unix calls to put the standard input into an unbuffered
 > state, or you can use Carbon calls to poll for events with WaitNextEvent
 > or use Carbon's GetKeys() to read the state of the keyboard, or you can
 > use CarbonEvent calls to register a callback that will be called on each
 > keyDown, or you can use Cocoa, and override the -(void)keyDown: handler,
 > which by default calls the -interpretKeyEvent: handler, or you can use
 > OpenGL's Glut to have keyDowns delivered to your callback, you can do an
 > optional install of X11 and use X11 callbacks to get at the keyboard, or
 > you can reference the keyboard directly using IOKit.
 >
 > That's 8 ways to do it. If you are just learning C, you should just type
 > the carriage return, and modify your code to ignore the extra character,
 > until you know more.
 >

A UNIX call or a gcc library or flag is mostly what I was hoping to find
for now since I'm only writing for the command line while learning C.
When I decide to start working with an API (probably Cocoa and/or GTK+)
I'll use the appropriate key event handler.

Thanks for the quick response. I've only recently come to this newsgroup
and everyone seems very helpful here.

Eric<!-- ~MESSAGE_AFTER~ -->
 >> Stay informed about: non-line-buffered key input in C 
Back to top
Login to vote
David Phillip Oste

External


Since: Apr 25, 2004
Posts: 1083



(Msg. 6) Posted: Wed May 25, 2005 4:55 am
Post subject: Re: non-line-buffered key input in C [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

In article <cpUke.137280$hu5.80112@tornado.texas.rr.com>,
Eric Pellegrin <epellegrin2.no.spam.please RemoveThis @sw.rr.com> wrote:

 > This is exactly the kind of help I was looking for. It worked fine. Now
 > I'll have to go through the stty man page to learn how to disable this
 > when I'm done.

and of course:

man tcsetattr




to learn how to do it from C.

--
David Phillip Oster<!-- ~MESSAGE_AFTER~ -->
 >> Stay informed about: non-line-buffered key input in C 
Back to top
Login to vote
Display posts from previous:   
   Macintosh computer (Home) -> Programmer Help All times are: Pacific Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]