home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.os.msdos.programmer
- Path: sparky!uunet!cis.ohio-state.edu!magnus.acs.ohio-state.edu!usenet.ins.cwru.edu!ncoast!brown
- From: brown@NCoast.ORG (Stan Brown)
- Subject: Re: Keyboard input processing -- rolling one's own?
- Organization: Oak Road Systems, Cleveland Ohio USA
- Date: Mon, 31 Aug 1992 02:06:29 GMT
- Message-ID: <BttsIw.4z1@NCoast.ORG>
- References: <715029154snx@Wellspring.COM>
- Lines: 77
-
- In article <715029154snx@Wellspring.COM> elliot@Wellspring.COM writes:
- >
- >In Borland C++ (3.0) and MSC, there is a routine called getch which
- >gets a character directly from the console. I can use this and
- >isatty() to write my own getc that does what I want. The problem is,
- >I would like to attempt to make this routine portable to other
- >compilers and to UN*X; i.e. using ANSIC or C++ constructs only.
- >getch() is _not_ ANSI.
- >
- >Is this possible?
-
- No, it is not possible. The keyboard is unknown to ANSI C. (Don't post
- disagreements until you've read the standard, please.) So there is in
- principle no ANSI-standard way to do anything that requires the presence
- of a keyboard.
-
- FWIW, Microsoft C has getch( ); I think most MS-DOS based compilers do.
-
- This topic (like so many) is well covered by the FAQ list in
- comp.lang.c. Here's the relevant excerpt:
-
- -----------------------------------------------------------------------
- 12.1: How can I read a single character from the keyboard without
- waiting for a newline?
-
- A: Contrary to popular belief and many people's wishes, this is not
- a C-related question. (Nor are closely-related questions
- concerning the echo of keyboard input.) The delivery of
- characters from a "keyboard" to a C program is a function of the
- operating system in use, and cannot be standardized by the C
- language. Some versions of curses have a cbreak() function
- which does what you want. Under UNIX, use ioctl to play with
- the terminal driver modes (CBREAK or RAW under "classic"
- versions; ICANON, c_cc[VMIN] and c_cc[VTIME] under System V or
- Posix systems). Under MS-DOS, use getch(). Under VMS, try the
- Screen Management (SMG$) routines. Under other operating
- systems, you're on your own. Beware that some operating systems
- make this sort of thing impossible, because character collection
- into input lines is done by peripheral processors not under
- direct control of the CPU running your program.
-
- Operating system specific questions are not appropriate for
- comp.lang.c . Many common questions are answered in
- frequently-asked questions postings in such groups as
- comp.unix.questions and comp.os.msdos.programmer . Note that
- the answers are often not unique even across different variants
- of a system; bear in mind when answering system-specific
- questions that the answer that applies to your system may not
- apply to everyone else's.
-
- References: PCS Sec. 10 pp. 128-9, Sec. 10.1 pp. 130-1.
-
- 12.2: How can I find out if there are characters available for reading
- (and if so, how many)? Alternatively, how can I do a read that
- will not block if there are no characters available?
-
- A: These, too, are entirely operating-system-specific. Some
- versions of curses have a nodelay() function. Depending on your
- system, you may also be able to use "nonblocking I/O", or a
- system call named "select", or the FIONREAD ioctl, or kbhit(),
- or rdchk(), or the O_NDELAY option to open() or fcntl().
-
- Steve Summit
- scs@adam.mit.edu
- scs%adam.mit.edu@mit.edu
- mit-eddie!adam.mit.edu!scs
-
- This article is Copyright 1988, 1990-1992 by Steve Summit.
- It may be freely redistributed so long as the author's name, and this
- notice, are retained.
- -----------------------------------------------------------------------
-
- --
- Stan Brown, Oak Road Systems brown@Ncoast.ORG
- "Self-esteem, n. An erroneous appraisement."
- "Self-evident, adj. Evident to one's self and to nobody else."
- --Ambrose Bierce
-