# Keyboard Codes



## hruodr (Jan 18, 2021)

What are the bytes I get reading stdin when I press a Key of the keyboard
after setting the terminal to raw? On what they depend? On the operating
system?

You can see them with my tcl script bellow. For example:

Ascii -> Asci byte
ä,ö,ü   -> their utf-8 (c3-a4, c3-b6, c3-bc)
up, down, right, left arrow -> 1b-5b-41, 1b-5b-42, 1b-5b-43, 1b-5b-44 ????
F1, Shift F1, Alt F1 -> 1b-4f-50, 1b-5b-31-3b-32-50, 1b-5b-31-3b-33-50 ???
F5, Shift F5, Alt F5, Shift Alt F5 -> 1b-5b-31-35-7e, 1b-5b-31-35-3b-32-7e, 1b-5b-31-35-3b-33-7e, 1b-5b-31-35-3b-34-7e ???
And so on



```
#!/usr/opt/bin/tclsh

fconfigure stdin -encoding binary

proc readk {} {
set ttyst [exec stty -g]
exec /bin/stty raw -echo <@stdin
append key [binary encode hex [read stdin 1]]
set n [chan pending input stdin]
while {$n} {append key -[binary encode hex [read stdin 1]]; incr n -1}
exec /bin/stty -raw echo <@stdin
exec /bin/stty $ttyst <@stdin
return $key}

# leave with ^D
while {[set k [readk]] ne 04} {puts $k}
```


----------



## SirDice (Jan 18, 2021)

Escape sequences. 0x1b = 27 = ESC.


----------



## hruodr (Jan 18, 2021)

But what are these escape sequences? Are they a standard?


----------



## SirDice (Jan 18, 2021)

hruodr said:


> Are they a standard?


Yes, they depend on the terminal settings. For ANSI for example: https://en.wikipedia.org/wiki/ANSI_escape_code
Xterm has it's own escape codes: https://www.x.org/docs/xterm/ctlseqs.pdf


----------

