home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!portal!dfuller
- From: dfuller@portal.hq.videocart.com (Dave Fuller)
- Subject: Re: getch
- Message-ID: <Bxnxps.53K@portal.hq.videocart.com>
- Organization: VideOcart Inc.
- X-Newsreader: Tin 1.1 PL3
- References: <1992Nov13.022217.13742@pasteur.Berkeley.EDU>
- Date: Fri, 13 Nov 1992 16:27:27 GMT
- Lines: 58
-
- c60c-2gf@web-2a.berkeley.edu writes:
- :
- : At home, I run a version of Turbo C on my IBM. There is a library function
- : with a prototype in conio.h called getch (and getche). Is there an
- : equivalent of this function on standard Unix systems. I'm on a Sun, I
- : believe (not a Sparc station though). This question may be really
- : obvious, but I am not quite sure how all the specifics with input/output
- : work on the system (i.e. x-window control etc.) and I can't quite seem
- : to find getch. The man page goes on and on abou'curses,' but I am still
- : pretty lost. Any help would be appreciated (in email, please). Thanks
- :
- : -David Simpson
-
- The getch() call is part of the curses package. What you are looking for
- is probably getc() getchar() or fgetc(). the g??? are macros and fgetc()
- is a function. getchar is just getc(stdin). getc and fgetc both take
- a file pointer (stdin in your case).
-
- the problem with these is that they do not get anything from stdio until
- the ENTER key
- is hit (or EOL or EOF, or whatever the delimiter is). What this means
- is that you get no immediate response to a key hit, AND it is echoed to
- the stdout. so to get 1 key , they may type 100 keys, see them all, and
- then alert the program that it has a key in the buffer AFTER all input
- is done and ENTER is pressed. This probably is not what you want.
-
- Curses lessons cannot be given here, but here is a quick layout that
- should help you.
-
- in the main() start out by
- initscr(); - start curses.
- cbreak(); - this makes each key available immediately instead of waiting
- for a delimiter to occur (like getc(), getchar() and fgetc())
- without this, keyboard info is buffered to tty.
- noecho(); - use this if you don't want the keypresses echoed to screen.
- keypad(stdscr, TRUE); turns the function keys, keypad translation on
-
- this should do it.
-
- now calls to getch() will return a keypress without delay, but any key
- hit will NOT be echoed to screen, thanks to noecho(). If you want to
- echo, take this out.
-
- at the end to shut off curses just use
-
- endwin()
-
- on a Sun using cc you need to add libraries curses and termlib.
- i.e: cc myprog.c -omyprog -lcurses -ltermlib
-
- don't forget to #include <curses.h> in all files that use getch();
-
- if you forget to use initscr() the program will coredump on you.
-
- Dave Fuller
- dfuller@portal.hq.videocart.com
-
-
-