I'm not sure if this is the answer to the question directly, but I think it is connected - I wandered where these codes came from, and I finally found:
It is a little hard to read at first for the left arrow, find βLEFT 4β in the βKeyβ column, and for the sequence that bash
sees, find the fifth column βkeymapβ - βnormalβ), where it is written as β[D 1b 5b 44β is three bytes ( 27, 91, 68) representing this key.
Searching for a stream How to read arrow keys on really old bash? - The UNIX and Linux forums inspired me to write a short single-line liner that resets key codes for keystrokes. Basically, you press the key, then Enter (to start the end of read
), and then use hexdump
to display what read
has saved (and finally press Ctrl-C to exit the loop):
$ while true; do read -p?; echo -n $REPLY | hexdump -C; done ?^[[D 00000000 1b 5b 44 |.[D| # left arrow 00000003 ?^[[C 00000000 1b 5b 43 |.[C| # right arrow 00000003 ?^[[1;2D 00000000 1b 5b 31 3b 32 44 |.[1;2D| # Shift+left arrow 00000006 ?^[[1;2C 00000000 1b 5b 31 3b 32 43 |.[1;2C| # Shift+right arrow 00000006 ?^C
So, while the arrow keys require 3 bytes - the Shift + arrow keys require 6! However, it would seem that all these sequences begin with 0x1b (27), so you could check this value for read -n1
before reading more bytes; also 5b
remains the second byte in the multibyte sequence for the "normal" and "shift / NUM-Lock" columns in the table above.
Edit: it is much easier and more correct to check terminal codes of keystrokes in Linux via showkey :
$ showkey Couldn't get a file descriptor referring to the console $ showkey -h showkey version 1.15 usage: showkey [options...] valid options are: -h
source share