#!/bin/sh
#
# #!/bin/dash
# #!/bin/bash
# #!/bin/ksh
# #!/bin/zsh

# Inkey_ADE.sh
# An "INKEY$" DEMO for the AMIGA A1200, using ADE and ADE's ksh[88] or sh
# inside a standard AMIGA shell.
# Also works on many other *NIX type terminals and fully POSIX compliant.

# Clear the screen.
printf "%b" "\033c\033[2J\033[H"

echo ''
echo 'INKEY$ for ADE. Read the code for more information!'
echo 'Just momentarily press an ASCII key only and it will display on screen...'
echo 'Press Q, (uppercase), to quit, (the RETURN/ENTER key is not needed)...'
printf "%b" "\033[7;28fYou pressed:-\n\n\n"

# 'KB' is the variable used INSIDE the Inkey function.
KB=''
Esc=$( printf "%b" "\033" )

# ************************************************
#         !!! THIS IS THE IMPORTANT BIT !!!
# ************************************************
# Create a function Inkey as a one liner.
# Called as:
# Inkey 2>&1 > /dev/null
# Returns a variable 'KB' with the key that is pressed.
#
Inkey() { KB=''; stty -echo -icanon min 1 time 0 2>&1 > /dev/null; KB=$( dd bs=1 count=1 2> /dev/null ); stty sane 2>&1 > /dev/null; }
#
# ************************************************
#            !!! IMPORTANT BIT END !!!
# ************************************************

# Now test by running.
while :
do
	# Call Inkey as shown above.
	Inkey 2>&1 > /dev/null
	printf "%bYou pressed:- %c          \n\n\n" "\033[7;28f" "${KB}"
	if [ "${KB}" = "Q" ]
	then
		break
	fi
	# Note even Escape is catered for...
	if [ "${KB}" = "${Esc}" ]
	then
		printf "%b" "\033[7;1fYou pressed the 'Esc' key, now exiting the program with a return code of 1!\n\n\n"
		exit 1
	fi
done

# Exit the script with a return code of 0!
printf "%b" "\033[7;28fYou '"${KB}"'uit the program!          \n\n\n"
exit 0