Yesterday I posted the following question:
Quote:>I have a program which needs to scan the keyboard for any
>key presses. Does anyone know how this is done in UNIX (AT&T
>System V enviornment) using a C program. I can't seem to find any
>UNIX system calls that do it and the input routines provided by C
>don't return until you enter a return character. Thanks for any
>help you may be able to provide.
Thanks to all people who responded. I have put together the following
program and routines for anyone else who might have the same question.
Omar
#include <stdio.h>
#include <fcntl.h>
#include <termio.h>
main()
{
int s;
char key;
init();
for(;;){
s = keyscan(&key);
if (s != 0) printf("You pressed '%c'\n",key);
else printf("no input\n");
if (key == 'q') break;
}
final();
Quote:}
static int stdinfd;
static struct termio tio_save;
static int ff_save;
init()
{
int ff;
struct termio tio;
/* Get the file descriptor for stdin and duplicate it */
stdinfd = fileno(stdin);
stdinfd = dup(stdinfd);
/* Get the termio structure and save it */
ioctl(stdinfd,TCGETA,&tio_save);
bcopy((char *)&tio_save,(char *)&tio,sizeof(struct termio));
/* Disable the ICANON and ECHO flags */
tio.c_lflag = tio.c_lflag&~(ICANON|ECHO);
/* Set minimum number of characters in buffer to zero */
tio.c_cc[4] = 0;
/* Set amount of time to wait for characters to zero */
tio.c_cc[5] = 0;
/* Change the termio structure for stdin */
ioctl(stdinfd,TCSETA,&tio);
/* Enable the no delay flag on stdin, so that read() is not blocked */
fcntl(stdinfd,F_GETFL,&ff_save);
ff = ff_save | FNDELAY;
fcntl(stdinfd,F_SETFL,ff);
Quote:}
final()
{
/* Restore the initial settings */
ioctl(stdinfd,TCSETA,&tio_save);
fcntl(stdinfd,F_SETFL,ff_save);
Quote:}
keyscan(c)
char *c;
{
int s;
s = read(stdinfd,c,1);
return s;
Quote:}