1

I've been rediscovering ASCII control characters as I fiddled with some terminal emulation code, and wrote a small program that outputs the hex value of each key I press (tcsetattr() with ~ICANON).

While I understand pressing the keys Control and D simultaneously (CTL-D) generates the ASCII ^D/EOT/0x04 control character, and a slew of others as nicely described on the WP ASCII page, I was surprised to see that (on OS X at least), some other unexpected keys generated control chars as well:

CTL-2/@ = NUL = ^@ = 0x00 (expected actually)
CTL-3/# = ESC = ^[ = 0x1B
CTL-4/$ = FS = ^\ = 0x1C
CTL-5/% = GS = ^] = 0x1D
CTL-6/^ = RS = ^^ = 0x1E (expected actually)
CTL-7/& = CTL-/ = US = ^_ = 0x1F
CTL-8/* = DEL = ^? = 0x7F

Note that CTL-1, -9, and -0 don't generate control characters, just the normal digit.

Except for those two expected chars (NUL and RS), why do terminals generate control characters for this subset of number keys?

Edit: to be clear, I'm aware and understand that CTL-[=Esc, and the other standard "caret"/control keys (^\, ^], ^^, ^_, ^?). I'm wondering about why the number or ^#,^$,^%,^&,^* and ^/ control keys overlap with them.

  • ^-[ = Esc is widely supported and is the most compatible way to send an Esc key to vim (even across multiple terminals using remote sessions, etc.). Ctrl-] is so widely recognized that even though Win95's Telnet.exe doesn't support 0x1D, it does support Ctrl-]. ^-? is not at all surprising, perhaps due to some old experimentation I've done. My main point is that I conclusively know, from familiarity, that at least some of these are not anywhere close to being OSX-specific. – TOOGAM Jan 06 '16 at 01:00
  • I'm well aware of ^[ =ESC and the other standard control chars, that's not what the question is about. – John de Largentaye Jan 06 '16 at 01:03

1 Answers1

0

Caret notation, like ^D, does not actually mean Ctrl-D. It means "The ASCII control character with the number matching the alphabetical rank of the letter" in that case, D=4 so ^D = 0x04. Some people decided it would be neat to map these caret notation to the letters of the keyboard of ctrl-X scheme. Obviously, it breaks at 27, or 0x1B which is where people needed to be creative.

My understanding is that there never was any standard to map control character superior to 0x1A and that various historical implementation have coexisted for a while.

YvesQuemener
  • 101
  • 1
  • 1
    Not at it matters really, but got a citation for "^D != CTL-D originally"? :) Wikipedia doesn't have one. – John de Largentaye Jan 06 '16 at 02:21
  • 1
    Actually, according to http://superuser.com/a/764656/541440, and digging into the history of Teletypes https://en.wikipedia.org/wiki/Teleprinter#Control_characters, it seems that from the very start it was meant for those control characters to be generatable using Ctrl+key – John de Largentaye Jan 06 '16 at 03:03