Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

kbhit() and getch()

0 views
Skip to first unread message

Adam Augusta

unread,
Jan 18, 1997, 3:00:00 AM1/18/97
to

Hello fellow Linusian disciples.
I used to program C in DOS but now that I attend WPI courses, I have seen
the light. One problem though. I use kbhit() and getch() often to avoid
scanning in characters with the enter key. Quite unfortunately, in UNIX,
kbhit() and getch() don't exist. Are there any UNIX substitutes?


-Lord Roxton==evil smiting, righteousness defending, ignoramus
extraordinaire

Jean-Pierre Radley

unread,
Jan 19, 1997, 3:00:00 AM1/19/97
to

Adam Augusta's CPU sent forth this bytestream:
| Hello fellow Linusian disciples.

Who be those???

| I used to program C in DOS but now that I attend WPI courses, I have seen
| the light. One problem though. I use kbhit() and getch() often to avoid
| scanning in characters with the enter key. Quite unfortunately, in UNIX,
| kbhit() and getch() don't exist. Are there any UNIX substitutes?

How do I read a character from a keyboard without waiting for a newline?

KEYWORDS: vmin vtime termio ioctl getchar terminals stty icanon terminal
keyboard getchar nonbuffered input nonblocking kbhit

RELEASE: SCO Development System (not specific to any SCO release)

PROBLEM: When I read a character from a keyboard, there must be a newline
before the read() (or getchar(), etc.) call returns. How can I
read just the desired character so a newline isn't required?

SOLUTION: For SCO XENIX, see the manual titled SCO XENIX System V Development
System C Language Guide, C Library Guide section, Chapter 6.
Page 6-40 documents an example program that is similar to the one
supplied below. Beware of the typo in it. The line that says:
"tio.c_iflag &= ~ICANON;" should be "tio.c_lflag &= ~ICANON;".

For SCO UNIX, see the manual titled SCO UNIX System V/386
Development System Library Guide, Chapter 6. Page 6-45 has an
example program that is similar to the one supplied below. There
are not any typos in this program example.

The code below demonstrates how to change the terminals settings.
main() demonstrates a simple use of the init() and endinit()
procedures, which actually change the tty state, specifically the
ICANON mode.

For more sample code that makes use of termio(M), see the files
/usr/lib/uucp/dial*.c. You must have the UUCP Operating System
package installed.


SEE ALSO: termio(M) ioctl(S) stty(C)
SCO UNIX System V/386 Development System Library Guide, Chapter 6.
SCO XENIX System V Development System C Language Guide C Library
Guide section, Chapter 6.

//*
** Compile: cc file.c
*/

#include <stdio.h>
#include <termio.h>
#include <sys/ioctl.h>

struct termio tty, oldtty;
main()
{
int c;

init(); /* Change the line characteristics of the keyboard */
printf("Enter 'q' to quit.\n");
while ((c = getchar()) != 'q' && c != EOF)
printf("\tYou typed <%c>\n", c);
endinit(); /* Reset keyboard */
putchar('\n');
}

init()
{
ioctl(0,TCGETA,&tty); /*Get parameters associated with terminal */
oldtty = tty; /*Duplicate to reinitialize later */
/*
** Change tty structure to get one character per read.
** Setting VTIME to zero makes a read wait until a character
** is received.
** If you want a non-blocking read use rdchk(S).
*/
tty.c_lflag &= ~ICANON; /*Set ICANON off */
tty.c_cc[VMIN] = (char) 1; /*Set VMIN to 1 */
tty.c_cc[VTIME] = (char) 0; /*Set VTIME to 0 */

ioctl(0,TCSETA,&tty); /*Set new parameters associated with tty */
}
endinit()
{
ioctl(0,TCSETA,&oldtty); /*Reset to default parameters */
}


--
Jean-Pierre Radley j...@jpr.com XC/XT Custodian Sysop, CompuServe SCOForum

0 new messages