Contribution: Checking for single Key press with Busybox ash

Harald Becker ralda at
Sun Jul 14 16:50:39 UTC 2013

Hi Rich !

On 14-07-2013 11:38 Rich Felker <dalias at> wrote:
>On Sun, Jul 14, 2013 at 01:25:52AM +0200, Harald Becker wrote:
>> >"read -r -t2 -n1 KEY" ?

>Waiting for a newline is a function of the terminal mode, not
>the read command. The default terminal mode buffers input at the
>kernel level and _nothing_ appears on the file descriptor until
>a newline is entered. In this case, the read command will still
>only consume one byte the rest will remain unread on the
>terminal device.

I know about tty handling/editing. Lets assume RAW mode without
any editing. The read command is a line oriented input. I
thought it always read data up to an NL. My knowledge was that
it is only possible to limit the maximum amount of data stored
into variables. Beside this, the -n option is a none standard
option and not available on all systems.
>> The idea behind my example is to have the possibility to check
>> for single key presses on every kind of tty and to catch even
>> special function key presses witch produce multi character
>> sequences. What about read when special control codes are
>> entered? How do you check for them in a shell script?
>If the first byte you read is an escape, you'd read more bytes
>until you've read a whole key code. You may want a timeout in
>case the user just manually pressed the escape key with nothing
>after it.

The classic function key handling. But what is the end of the
Function key sequence. How can you configure the keys your
terminal provide, etc.

Your example need a lot of programming code around the read. My
approach tries to handle this just using stty settings, with a
single read and the possibility to use a simple table file to
translate the entered key. This shall work on most tty
implementations and I even tried it over ssh connections, giving
a better response to single Escape / function key press then
other implementations.

Look at the menu example and tell me how the read looks for your
approach for configurable function key sequences (e.g. different


More information about the busybox mailing list