In older keyboards, enter and return are 2 different keys. Old Macintosh keyboards set it up so that the Return sends a carriage return \r
, while the Enter key sends a linefeed \n
. However many computers nowadays do not have separate Return and Enter keys. So what do these keyboards send? While it actually depends on your operating system, its keyboard settings, and the application you're interacting with. Windows applications will generally interpret \r\n
as a newline, while Mac OS 9 and earlier interpreted \r
as a newline, and finally Unix with Mac OS X and later will interpret \n
as a newline. People involved in the technology space will generally use the convention of \n
as the newline, with \r
as an artifact of computing history.
A similar situation occurs with the Backspace and Del keys. Modern convention has settled on the behaviour where Backspace deletes the character on the left of the cursor, and Del deletes the character under the cursor in the case of a block cursor, or to the right of the cursor in the case of a line cursor. However to achieve this behaviour 3 different codes are used in different ways by different applications in different operating systems: ASCII DEL \x7f
, ASCII BS \x08
and \x1b[3~
. The latter code is not an ASCII code, but a VT220 "escape code" which was to simulate the act of moving the cursor to the right and deleting the character on the left.
Testing all of this on my current computer, this is what my current keyboard represents on a Windows machine running picocom
via Cygwin (Note that I do not have a Return key):
Keyboard | Hex | Control Code (Caret Notation) | ASCII Description | Printable Representation |
---|---|---|---|---|
Backspace | 0x7f |
^? |
"DEL - Delete" | \x7f |
Delete | 0x1b[3~ |
^[[3~ |
None | \e[3~ |
Ctrl + H | 0x08 |
^H |
"BS - Backspace" | \b |
Enter | 0x0d |
^M |
"CR - Carriage Return" | \r |
Ctrl + M | 0x0a |
^J |
"LF - Line Feed" | \n |
Notice the confusing part where BACKSPACE is mapped ASCII DEL, while Del is mapped to a special character code, and finally ASCII BS is only available through a special control code sequence.
The solution to this madness, is to focus on 1 convention, and map all of your keyboard keys to the convention you specified, for all your applications. For some applications (most graphical/proprietary applications) this is not possible, but for many terminal applications this is easy once you understand the relevant code mappings. I sugggest you start by looking up ~/.inputrc
.
Understand that these competing conventions does result in input confusion especially when dealing with communication protocols and terminal devices. So always make sure when you're using applications like telnet
or picocom
to check what your keyboard is really sending. To learn more about this see stty
and the manual of the interactive application you're using.
Thank you for compiling this - I can't tell you how maddening it has been writing Python console applications that run on both Mac and Raspberry Pi and having to support multiple keycodes for certain keys. Sorted them out eventually; but the underlying reasons still nagged at me! Still bothered by "backspace" being ASCII DEL and "del" being a special code... Oh, legacy standards...