Updated 2011-10-19 11:30:14 by dkf

AJB - I wrote this while working on code to handle menus, and editors, in the raw console mode. It will simply print out the hex code for the given key sequence. Press ESC twice to exit. It will work on linux varients, I assume it will work on unix. It does rely on having the stty utility. A bit of warning: different terminals often have different scan codes for complex sequences, ie: linux console versus xterm console.
 exec stty raw -echo
 fconfigure stdin -buffering none -blocking 1
 fconfigure stdout -translation crlf
 while 1 {
   set key [format "0x%02X" [scan [read stdin 1] %c]]
   if {$key == 0x1B} {
     set term 0
     while {(! [fblocked stdin] ) && $term == 0} {
       set chr [format "0x%02X" [scan [read stdin 1] %c]]
       if {$chr >= 0x41 && $chr <= 0x48} {set term 1}
       if {$chr >= 0x50 && $chr <= 0x53} {set term 1}
       if {$chr == 0x7E} {set term 1}
       if {$chr == 0x1B} {exec stty -raw echo; exit}
       lappend key $chr
   puts $key
   flush stdout
   after 20

example: this is the output given for a up arrow, down arrow, left arrow, and right arrow:
 0x1B 0x5B 0x41
 0x1B 0x5B 0x42
 0x1B 0x5B 0x44
 0x1B 0x5B 0x43