I want to know which keys are pressed on my keyboard and print the information to stdout.
A tool that can do this is showkey
. However, if I want to pass the data of showkey
to read
:
while read line; do
echo "$line" | otherprog
done <`showkey -a`
OR
showkey -a | while read line; do
echo "$line" | otherprog
done
Then showkey
waits until a sum of 140 characters is typed in and then sends the buffered information to read
.
showkey -a
prints the pressed keys line by line, without any buffering.
- Why does it buffer?
- How do I avoid this buffering, so that I can read
showkey
's output truly line by line? - Is there an alternative to
showkey
? - Is there a file I can read the pressed keys directly from?
- What is the correct way to pass data to
read
?
Solution:
I've used lornix's solution and included it into my simple keyboard keyboard :D!
stdbuf -o0 showkey -a | while read line; do
perl -e 'print sprintf "%030s\n",shift' "$line" | aplay &> /dev/null &
done
Lasership version:
#!/bin/bash
MP3=(); for i in mp3/*.mp3; do MP3+=("$i"); done
NMP3=${#MP3[@]}
stdbuf -o0 showkey -a 2>/dev/null | while read line; do
[ -z "$line" ] || ! [[ $line =~ ^[0-9] ]] && continue
NUM="$(echo "$line" | awk '{print $2}')"
mplayer "${MP3[$(($NUM % $NMP3))]}" &>/dev/null &
done
In the same folder, download some laser mp3 files into a folder called mp3
.