C+
C 	INTEGER FUNCTION IESC
C
C	This function when invoked checks to see if the keyboard has been
C	hit.  It returns a TRUE/FALSE indicator and optionally the character
C	typed in the dummy variable which is an optional input.  The function
C	when called sets up the terminal for recieving unsolicited input.
C
C	Usage:		Istatus = IESC()	or
C			Istatus = IESC(IDUMY)
C
C	The first form is allowed because IESC does a call to IADDR to see
C	if IDUMY is present in the argument list.  IDUMY is only returned
C	if it was present.
C
C	Event Flags:	Uses EFN=2 for QIO
C
C	Written by:	Pete J. Zima
C	Modified by:	James G. Downward	Allow IDUMY to be optional by
C						checking the # of input args.
C
	FUNCTION IESC (IDUM)
	IMPLICIT INTEGER*2 (A - Z)		! Fcn  works with /NOI4 prog
	LOGICAL*1 FLAG				!
	INTEGER*2 IESC_EFN			!
	INTEGER*2 IOSB(4)			! For QIO
	INTEGER*2 IESC_LEN			!
	INTEGER*2 IESC_BUF(4)			!
	INTEGER*2 IESC_CHAN			!
	CHARACTER*2 TERMINAL /'TT'/		!
	EXTERNAL IO$_SENSEMODE			!
	EXTERNAL IO$M_TYPEAHDCNT		!
	EXTERNAL IO$_READVBLK			!
	EXTERNAL IO$M_TIMED			!
	EXTERNAL IO$M_NOECHO			! Read without echoing
	DATA IESC_BUF / 0,0,0,0 /		!
	DATA FLAG / .FALSE. /			!
	Narg=IADDR(IDUM)			! If missing address=0
	IESC = 0				!
	IF(Narg.GT.0)   IDUM = 0		! Only set if arg is present
	IESC_LEN = 8				!
	IESC_EFN = 2				! Event flag for QIO
	IF (FLAG) GOTO 10			! Only assign chnl first time
	ISTAT = SYS$ASSIGN (TERMINAL,IESC_CHAN,,)! Assign I/O channel
	IF (.NOT. ISTAT) CALL LIB$STOP (%VAL(ISTAT))! Stop if assign fails
	FLAG = .TRUE.				! Show we have assigned chnl
10	IO_FUNC = %LOC(IO$_SENSEMODE) .OR. %LOC(IO$M_TYPEAHDCNT)
	ISTAT = SYS$QIO (%VAL(IESC_EFN),	! Event flag #2
     -			 %VAL(IESC_CHAN),	! Channel for TT from assign
     -	 		 %VAL(IO_FUNC),		! I/O funct: senseVtypeahead
     -			 %REF(IOSB),		! Error info here
     -			 ,			! AST routine to enter - null
     -	 		 ,			! AST parameter - null
     -			 %REF(IESC_BUF),	! Character buffer
     -			 %VAL(IESC_LEN),	! Length of character buffer
     -			 ,			! Seconds for timeout
     -			 ,			! Default terminator is CR
     -			 ,			! Prompt message - null
     -			 ,)			! Length of prmpt messge: null
	IF (.NOT. ISTAT) CALL LIB$STOP (%VAL(ISTAT)) ! Stop if QIO not success
	IF (IESC_BUF(1) .GT. 0) THEN
		IESC = 1			! Signal keyboard entry
		IF(Narg.GT.0)IDUM = IESC_BUF(2)	! and return first character
	ENDIF
C	WRITE (6,100) IESC,IDUM
C100	FORMAT (6X,'Byte count: ',I4,' First character: ',A1)
	IF (IESC .EQ. 0) GOTO 20		! Return without flushing
C
C  The keyboard has been touched.  Some number of characters have are now
C  in the type ahead buffer.  These characters must be flushed from the buffer
C  with a read with a timeout of 0 so as not to delay.
C
	IESC_LEN = IESC_BUF(1)			! Length of character buffer
	IESC_TIME = 0				! 0 seconds to timeout
	IO_FUNC = %LOC(IO$_READVBLK) .OR. 	! Read char from terminal
     -		  %LOC(IO$M_TIMED)   .OR.	!   with no timeout
     -            %LOC(IO$M_NOECHO) 		!   and no echo
	ISTAT = SYS$QIO (%VAL(IESC_EFN),	! Event flag #2 also
     -			 %VAL(IESC_CHAN),	! Channel for TT from assign
     -			 %VAL(IO_FUNC),		! I/O function: readVtimed
     -			 %REF(IOSB),		! Error status here
     -			 ,			! AST routine to enter - null
     -			 ,			! AST parameter to use - null
     - 			 %REF(IESC_BUF),	! Character buffer
     -			 %VAL(IESC_LEN),	! Length of character buffer
     -			 %VAL(IESC_TIME),	! Sec for timeout: 0 purges
     -			 ,			! Default terminator is CR
     -			 ,			! Prompt message: null
     -			 ,)			! Length of message: null
	IF (.NOT. ISTAT) CALL LIB$STOP (%VAL(ISTAT)) ! Stop if not success
20	RETURN
	END

