While my code reads and displays lines from a file I want to detect if j
or k
keys are pressed. This will increase or decrease the rate at which the words are displayed to the screen.
I am not sure how to go about this. I did a bit of research and the only thing I found was using the read
command to listen for input from a user but how would I have the read
command always listening while the script is in execution?
I also found a trap
command but I think that is only to end the program?
I use the trap command to exit when ctrl-c
is pressed. Is it possible to use the trap command to listen for j
or k
keys as well?
any suggestions would be appreciated
atm I have this piece of code but it doesn't work the way I need it to. The script usage is: script file.txt [speed]
#!/bin/bash
trap end INT
file=$1
wpm=$2
input=$2
filecheck() {
#some code
}
initialspeed() {
#some code
}
fileread() {
#some code
}
display() {
#some code
}
end() {
#some code
}
#INCREASE OR DECREASE WPM SPEED
jk() {
input=$2
read -s -n 1 key
if [ "$key" == "k" ]
then
echo K HIT
exit
input=$input+10
wpm=$(bc <<< "scale=2;60/$input")
if [ "$input" -gt 1000 ]
then
wpm=0.06
fi
fi
if [ "$key" == "j" ]
then
echo J HIT
input=$input-10
wpm=$(bc <<< "scale=2;60/$input")
if [ "$input" -lt 100 ]
then
wpm=0.6
fi
fi
}
#CALL FUNCTIONS
echo
filecheck
initialspeed
fileread
jk #IM not sure where to call this function to make it listen all the time
display
EDIT: This is not a duplicate question because my problem isn't that I need to read input without waiting for the enter key to be pressed. I need to detect input at ANY point in the script execution. I already use -n 1
to read only 1 key press