Skip to content

Instantly share code, notes, and snippets.

@Bug-Reaper
Created August 26, 2025 08:43
Show Gist options
  • Select an option

  • Save Bug-Reaper/460db001545d17d0ec1f4b19f9c82009 to your computer and use it in GitHub Desktop.

Select an option

Save Bug-Reaper/460db001545d17d0ec1f4b19f9c82009 to your computer and use it in GitHub Desktop.
Example Bash Monitoring of `showkey` Utility
#!/bin/bash
STOP=0
trap 'echo "Caught Ctrl+C, cleaning up..."; STOP=1; cleanup; exit 0' SIGINT
cleanup() {
sudo pkill -f "showkey" 2>/dev/null
sudo pkill -f "script.*showkey" 2>/dev/null
pkill -P $$ 2>/dev/null
[[ -n $COPROC_PID ]] && kill $COPROC_PID 2>/dev/null
}
while [ $STOP -eq 0 ]; do
echo "Starting showkey monitoring..."
# Start coprocess
coproc sudo script -q -c "showkey" /dev/null 2>&1
while [ $STOP -eq 0 ] && kill -0 $COPROC_PID 2>/dev/null; do
if read -r -t 0.1 line <&${COPROC[0]} 2>/dev/null; then
echo "$line"
if [[ $line =~ keycode[[:space:]]+([0-9]+)[[:space:]]+(press|release) ]]; then
keycode="${BASH_REMATCH[1]}"
action="${BASH_REMATCH[2]}"
echo "Keycode: $keycode, Action: $action"
if [[ $keycode -eq 30 && $action == "press" ]]; then
echo "Pressed A!"
fi
fi
fi
done
cleanup
if [ $STOP -eq 0 ]; then
echo "showkey exited, restarting..."
sleep 1
fi
done
echo "Exited cleanly."
fi
done
echo "Exited cleanly."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment