| 1 | #!/bin/bash |
| 2 | # dd-keypress.sh: Capture keystrokes without needing to press ENTER. |
| 3 | |
| 4 | |
| 5 | keypresses=4 # Number of keypresses to capture. |
| 6 | |
| 7 | |
| 8 | old_tty_setting=$(stty -g) # Save old terminal settings. |
| 9 | |
| 10 | echo "Press $keypresses keys." |
| 11 | stty -icanon -echo # Disable canonical mode. |
| 12 | # Disable local echo. |
| 13 | keys=$(dd bs=1 count=$keypresses 2> /dev/null) |
| 14 | # 'dd' uses stdin, if "if" (input file) not specified. |
| 15 | |
| 16 | stty "$old_tty_setting" # Restore old terminal settings. |
| 17 | |
| 18 | echo "You pressed the \"$keys\" keys." |
| 19 | |
| 20 | # Thanks, Stephane Chazelas, for showing the way. |
| 21 | exit 0 |