home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
rtsi.com
/
2014.01.www.rtsi.com.tar
/
www.rtsi.com
/
OS9
/
OSK
/
TELECOM
/
stg_v4.lzh
/
inkey.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-11-11
|
2KB
|
111 lines
/*
* inkey -- get key from keyboard
*
* INPUT:
* iFlag -- 0=don't wait, 1=wait, -1=reset don't wait mode
*
* OUTPUT:
* returns (int) character (0=no key pressed)
*
* ERROR:
* (none)
*
* FUNCTIONS CALLED:
* function
*
* FUNCTIONS CALLED BY:
* function
*
* PROGRAMMER: Scott t. Griepentrog
* DATE started: 91/
* VERSION: 0.0
* MODIFICATIONS: 91/
*
*/
static char *RCSTAG="$Id: inkey.c,v 2.0 1992/08/10 18:48:23 mike Exp $";
#include "siv.h"
#include <stdio.h>
#include <termio.h>
#define ERR (-1)
inkey(iFlag)
int iFlag;
{
struct termio sTermIO;
static struct termio sTermIOsav;
static int iNoWait=0;
char ch;
int i;
if (iFlag==ERR) /* caller exits before next char pressed */
{
if (ioctl(0,TCSETAF,&sTermIOsav)==ERR) exit(perror("ioctl TCSETAF"));
iNoWait=0;
return(0);
}
if (!iFlag && !iNoWait) /* set for no waiting */
{
if (ioctl(0,TCGETA,&sTermIO)==ERR) exit(perror("ioctl TCGETA"));
if (ioctl(0,TCGETA,&sTermIOsav)==ERR) exit(perror("ioctl TCGETA"));
sTermIO.c_lflag&=~ICANON;
sTermIO.c_cc[4]=0; /* minimum # of chars */
sTermIO.c_cc[5]=0; /* maximum amt of time */
sTermIO.c_lflag=0; /* no echo */
if (ioctl(0,TCSETAF,&sTermIO)==ERR) exit(perror("ioctl TCSETAF"));
iNoWait=1;
}
i=read(0,&ch,1);
/* 0=no key pressed */
if (i==0)
return(0);
/* ignore any errors */
if (i==ERR)
return(0);
if (iNoWait) /* set back to normal since we have character */
{
if (ioctl(0,TCSETAF,&sTermIOsav)==ERR) exit(perror("ioctl TCSETAF"));
iNoWait=0;
}
return(ch);
}
/*
* sample test routine
*/
#ifdef MAIN
main()
{
int c;
printf("Press Escape to Quit...\n");
c=0;
while (c!=27)
{
c=inkey(0);
printf("[%d]\n",c);
/* if got a char, call inkey again right away */
if (c) continue;
write(1,"\7",1);
sleep(1);
}
}
#endif