Reputation: 1736
I have this short script:
until [[ $ACTION == "t" ]]; do
read -sn1 ACTION
case $ACTION in
e|E) printf "£\n";;
done
When I press "e", a pound sign is printed. When I press and hold "e", I get a continuous stream of "£", one under another.
Is there a way to do this: press and hold "e" resulting in only one "£" printed, until the "e" is released and pressed again?
And here is the catch: without the TIMEOUT "-t" switch in read
Please help :)
============= EDIT one day later ==============
The problem I want to solve is this: I can move a symbol on the screen using a combination of tput commands. When the TIMEOUT is set in the read command, two things can happen: either the TIMEOUT is big enough, and holding down a key does not move the symbol continuously - but then quick tapping on the key corrupts the screen; or if the TIMEOUT is small, quick tapping does not corrupt the screen, but holding down the key produces ALARM CLOCK interrupt. So, I don't want Bash to wait for input. I want it to read a pressed arrow key once, and that's it - user needs to release the key and press again to continue moving.
I have been using this function below, but it's producing all these problems:
ReadKey() {
# Wait for first char
if read -sN1 ACTION; then
# Read rest of chars
while read -sN1 -t 0.05 ; do
ACTION+="${REPLY}"
done
fi
}
and then
while Readkey; do
case $ACTION in
[case options follow]
Upvotes: 3
Views: 470
Reputation: 1736
I have come to the conclusion that this is a Windows problem. I have run my game on Ubuntu and it works like a dream.
Upvotes: 1
Reputation: 1449
Maybe you can achieve your goal using stty, something like this should work for your scenario:
stty -echo -icanon time 1 min 0 ;
ACTION=''
until [[ $ACTION == "t" ]]; do
read ACTION
case $ACTION in
e|E) printf "£\n";;
esac
done
stty sane;
I'm not sure if this will corrupt your screen when "quick tapping" is used, but if that is the case, provide details about such corruption and I'll try to help you.
Upvotes: 0
Reputation: 123510
You can use the DECARM extension on supported terminals to disable auto-repeat:
printf '\e[?8l'
To re-enable auto-repeat, use printf '\e[?8h'
Upvotes: 0