This is probably common knowledge to the professionals but not so much for amateurs like
myself.
This is a code snippet for the equivalent of BASIC's...
LET char$=INKEY$
As the timeout parameter cannot be less than 1 second then this is the only limitation...
It is a single line function which has a variable "char"...
Read the code for more information...
Issued as Public Domain...
#!/bin/bash
# An INKEY$ function for bash!
inkey() { char="" ; read -p "" -n1 -s -t1 char ; }
# Similar to BASIC's LET char$=INKEY$
# Do you remember INKEY$ in BASIC programming?
# Example:-
#
# PRINT "Some prompt:- "
# some_label:
# LET char$=INKEY$
# IF char$="<some_character>" THEN <do_something>
# IF char$="" THEN <do_something_else>
# GOTO some_label
# This is just a test piece only...
while true
do
printf "Some prompt:- "
# This is LET char$=INKEY$...
inkey
printf "Key pressed:- '$char'...\n"
if [ "$char" == "q" ]
then
printf "Quitting... \n"
break
fi
if [ "$char" == "" ]
then
printf "Timeout works OK...\n"
fi
if [ "$char" == "b" ]
then
printf "Barry Walker...\n"
fi
done
What read -n 1 is doing here is probably something like stty -icanon min 1 ; get_character ; reset
...since it's not BASH that's preventing you from reading one at a time, but the terminal itself. Usually it's in "canon" mode, or line-by-line.
Hi Corona688...
Thanks for the info...
One day I will become a coder... ;o)
I have used a derivative of it in the next stage of the AudioScope as a pseudo vertical shift control, so I thought I would let other people like me have it...
What's wrong with
stty raw -echo
inkey=`dd bs=1 count=1 2>/dev/null`
stty -raw echo
...that someone excellently posted a while back. This can be tweaked for particular shell, but doesn't rely on the shell's inbuilt functions and is less susceptible to the variations across OS flavours.
Bring back the ZX-Spectrum (Ran out of colours!)
Robin
For one thing, it assumes that a keystroke is one character(frequently untrue).
For another, it runs stty twice per keystroke... I'd be tempted to leave the terminal as is instead of changing it that often, save you a lot of CPU time.
This should work work:
stty -icanon min 0 time 1
key=`dd count=1 2>/dev/null`
...
It should be able to read entire keystrokes at once.
Hi rbatte1...
It is NOT a callable one liner function?
;o)
Thanks to Corona688 and his knowledge...
Here we go a single line function:-
#!/bin/bash
inkey() { char="" ; stty -icanon min 0 time 1 ; char=`dd count=1 2>/dev/null` ; }
while true
do
printf "Some prompt:- "
# This is LET char$=INKEY$...
inkey
printf "Key pressed:- '$char'...\n"
if [ "$char" == "q" ]
then
printf "Quitting... \n"
break
fi
if [ "$char" == "" ]
then
printf "Timeout works OK...\n"
fi
if [ "$char" == "b" ]
then
printf "Barry Walker...\n"
fi
done
Bazza...