I use stty raw -echo
in my tcl terminal program.
What I want is to do some actions for only a few key press events such as Tab. For the rest of the key press events I want to delegate to rlwrap or other default handling program, such as ← to move cursor to left hand side and insert text in where the cursor is, or Ctrl+C to terminate the program. Is there a way to do it?
How to delegate key press handling to rlwrap in raw mode terminal with tcl
396 views Asked by Herbert AtThere are 3 answers
Alas, terminal handling systems don't work in ways that make that easy. But it isn't impossible. The simplest mechanism I can think of (indeed, the only one that doesn't make me cringe at the thought) is to use some of the more advanced features of the Expect extension's interact
command.
In particular, interact
effectively connects the program spawn
ed by Expect to the outside world, yet you can also add patterns to allow extended behaviour.
package require Expect
spawn /your/program yourarguments...
interact {
"\t" {
# Do something special here as we've got a Tab
send "special special special\r"
}
}
You can use rlwrap on the spawned process:
spawn rlwrap /your/program yourarguments...
The latest GitHub version of rlwrap
has a bindable readline command rlwrap-direct-keypress
that can be bound (e.g. in your .inputrc
) to any key you want to pass on directly to your program, bypassing rlwrap
All other keys will still work as usual (e.g. moving the cursor) when editing an input line with readline
There is an option in stty that handles signals for you which is the
isig
option. Also,-opost
process "\n" to "\r\n" for you.Another choice is to use explicit
-icanon min 1 time 0
instead ofraw
option.