Run this:
puts "Type any characters, then <Return>." expect { ? { set result $expect_out(0,string) if {[string compare \n $result]} { scan $result %c value if {$value==27} { puts "You just typed Esc (decimal ASCII 27)." } { puts "You just typed '$result' (decimal ASCII $value)." } } exp_continue } }
Example output: type "a<CR>", see decimal 97; type "<F1><CR>", see the sequence
27-91-49-(49,50)-126 [explain] Win 95
Also see "keysyms", CWIND, and "How to send escape characters through Expect".
Csan The original code did not handle the Esc $result properly - it ate the second ' character in the output. I added a branch to handle that case.
I also extended the output to include the octal and hexadecimal values - useful for further re-use of the $result - see further on. Another addition was to save the resulting character (sequence) to keyseq.dat, which can be imported into another text editor. I find this addition very useful while using 'joe' editor...
The new version of the script (I called it keyseqs.exp):
#!/usr/bin/expect puts "Type any characters followed by <Return>, wait 10 seconds or press Ctrl-C." expect { ? { set result $expect_out(0,string) if [string compare \n $result] { if {[catch {eof $fd}]} { set fd [open keyseq.dat w] } scan $result %c dvalue if {$dvalue==27} { puts "You just typed Esc (ASCII octal 033, decimal 27, hexadecimal 1B)." } { puts "You just typed '$result' (ASCII octal [format %4s [format %#o $dvalue]], decimal [format %3s $dvalue], hexadecimal [format %x $dvalue])." } puts -nonewline $fd $result } { if {![catch {eof $fd}]} { close $fd puts "Character (sequence) written to keyseq.dat (after flushing the file contents)." puts "\nType any characters followed by <Return>, wait 10 seconds or press Ctrl-C." } } exp_continue } }
Example output (I typed PageUp):
# ./keyseqs.exp Type any characters followed by <Return>, wait 10 seconds or press Ctrl-C. ^[[5~ You just typed Esc (ASCII octal 033, decimal 27, hexadecimal 1B). You just typed '[' (ASCII octal 0133, decimal 91, hexadecimal 5b). You just typed '5' (ASCII octal 065, decimal 53, hexadecimal 35). You just typed '~' (ASCII octal 0176, decimal 126, hexadecimal 7e). Character (sequence) written to keyseq.dat (after flushing the file contents). Type any characters followed by <Return>, wait 10 seconds or press Ctrl-C. #
LV says: "Believe it or not, there is no set standard for what function keys generate. In fact, depending on the application, the same function key could generate different values [for different applications] on some systems."