include	clib.inc
include	conio.inc

	public	getkey
	public	getesc

_TEXT	SEGMENT

keystroke PROC
	mov	ax,keyboard	; 01h: CHECK FOR KEYSTROKE
	inc	ah      	; 11h: CHECK FOR ENHANCED KEYSTROKE
	int	16h
	jz	keystroke_end	; ZF clear if keystroke available
	mov	ax,keyboard	; 00h: GET KEYSTROKE
	int	16h		; 10h: GET ENHANCED KEYSTROKE
	;-----------------------
	or      ah,ah		; Yury - 04 Apr 2012
	jz      @F		; - Russian letter 224 (0xE0) discarded
	cmp	al,0E0h
	jne	keystroke_end
	xor     al,al
      @@:
	or      ax,ax
	;------------------------
    keystroke_end:
	ret
keystroke ENDP

getkey PROC DIST
	call	keystroke
	jnz	getkey_end
	test	console,CON_INT2F
	jz	getkey_nul
	mov	ax,1680h; added 05/05/2008 (bttr)
	int	2Fh     ; RELEASE CURRENT VIRTUAL MACHINE TIME-SLICE
    getkey_nul:
	xor	ax,ax
    getkey_end:
	test	ax,ax
	ret
getkey ENDP

getesc PROC DIST
	call	keystroke
	jz	getesc_nul
	cmp	ax,011Bh	; ESC
	je	getesc_end
    getesc_nul:
	xor	ax,ax
    getesc_end:
	test	ax,ax
	ret
getesc ENDP

Install	PROC
	xor	ax,ax
	mov	es,ax
	inc	ax
	mov	keyboard,ax
	mov	ah,12h
	int	16h
	mov	bx,0417h
	cmp	al,es:[bx]
	jne	Install_2F
	mov	ah,80h
	xor	es:[bx],ah
	mov	ah,12h
	int	16h
	cmp	al,es:[bx]
	jne	Install_2F
	mov	ax,1001h
	mov	keyboard,ax
    Install_2F:
	test	console,CON_INT2F
	jz	Install_end
	and	console,not CON_INT2F
	mov	ax,1680h
	int	2Fh
	test	al,al
	jnz	Install_end
	or	console,CON_INT2F
    Install_end:
	ret
Install	ENDP

_TEXT	ENDS

_DATA	SEGMENT
keyboard dw	0
_DATA	ENDS

_INIT_	SEGMENT
	dw	Install
_INIT_	ENDS

	END
