ANSI Escape Code

The escape sequence like ^[[A or \e[A are part of a terminal's ANSI Escape Codes which represent keyboard input or control sequences. For arrow keys, they typically correspond to specific sequences sent by the terminal when keys are pressed.

To see the escape sequence code of a key, use showkey -a in a terminal and press key you want. It will show like below on pressing arrow keys. Note: Ctrl-D will terminate the program, Ctrl-C would not work.

[user@hostname ~]$ showkey -a

Press any keys - Ctrl-D will terminate this program

^[[A 	27 0033 0x1b
 	91 0133 0x5b
 	65 0101 0x41
^[[B 	27 0033 0x1b
 	91 0133 0x5b
 	66 0102 0x42
^[[D 	27 0033 0x1b
 	91 0133 0x5b
 	68 0104 0x44
^[[C 	27 0033 0x1b
 	91 0133 0x5b
 	67 0103 0x43
^D 	 4 0004 0x04
[user@hostname ~]$

And cat -v also can be used. It will show like below on pressing arrow keys.

[user@hostname ~]$ cat -v
^[[A^[[B^[[D^[[C

See also Keyboard Input for Arch Wiki

To continue about colorizing output in terminal using escape codes or escape sequence.

Comments

Popular posts from this blog

printf - Printing with format

rebase or no-rebase for git pull

dmmsh-wlr for managing outputs in Wayland using wlr-randr