Can anyone suggest a simple way to do "press any key to continue" in
Tcl?
Thanks!
http://wiki.tcl.tk/14693 - Reading a single character from the
keyboard from Tcl...
THanks!
But I didn't expect it would be so complicated.
puts "Press any key to continue
gets stdin
> Whats wrong with
>
> puts "Press any key to continue
> gets stdin
The wrong is, that it should be "Press Enter/Return to continue" rather... ;)
--
ZB
On windows, that's a console setting. Even with 'fconfigure stdin
-buffering none', the commandline editor for the console itself is still
in cooked mode rather than raw.
I could have sworn a raw/cooked switch for the console channel driver
has come up in discussions before. It would be easy to do. IOW, place
stty options in the channel driver.
Why aren't they there now?
This one always gets me: -buffering sets the *output* buffering only, so
has absolutely no effect on stdin. I wish that had been named
-outputbuffering or something, as I frequently make this mistake. Of
course, Tcl buffers all input until you gets/read it.
the commandline editor for the console itself is still
> in cooked mode rather than raw.
>
> I could have sworn a raw/cooked switch for the console channel driver
> has come up in discussions before. It would be easy to do. IOW, place
> stty options in the channel driver.
>
> Why aren't they there now?
http://tip.tcl.tk/160 - has been in Vote: Pending status for a very long
time.
-- Neil
A simpler way is to adapt the following code posted by Bryan Oakley in
response to a question of mine 2 or 3 weeks ago
Gerry
------- The following was by Bryan Oakley
Here's code to show how you can trap all keypresses, reporting back
the
keysym, keycode, unicode character and keyboard state. In this
example
nothing ever appears in the text widget. Remove ";break" from the
binding to have the widget reflect what you typed.
This is by no means the only way to accomplish this task. You might
also
want to read up on bindtags. With just a couple of lines of code you
can
swap in or out whole sets of bindings.
proc main {} {
text .t -wrap word -yscrollcommand [list .vsb set]
scrollbar .vsb -command [list .t yview]
pack .vsb -side right -fill y
pack .t -side left -fill both -expand true
# The "break" is to inhibit the default behavior
bind .t <Any-KeyPress> {keyPress %k %K %s %A;break}
}
proc keyPress {keycode keysym state unicode} {
puts "you pressed a key:\
keycode=$keycode keysym=$keysym\
state=$state unicode=$unicode"
}
main
-------------------------------
I have the same problem but with the additional constraint, that stdin
is not a terminal, i.e. stty will not work. stdin is a pipe and I just
can't turn of the line buffering.
Any ideas there?
Bye, Fabian
--
Das Wer klobt seinen Meister!
That's an entirely different problem: if you're using a pipe or socket
and [read $ch 1] doesn't return, it means that the byte was not
written by the writer. Your read side won't be able to change a bit of
this.
Now if you can put your fingers inside the guts of the writer, a
frequent mistake is a lack of flushing. If it is written in Tcl too,
it' s just a matter of [flush] or [fconfigure -buffering none]. For C
it is fflush/setvbuf.
-Alex
The hard part is because the stdin is a terminal. Otherwise turning
off "line buffering" is easy:
fconfigure stdin -translation binary
but remember to use [read] instead of [gets]. To do a "press any key"
type thing for ordinary channels simply do:
read stdin 1
Or you can use the event loop:
fileevent stdin readable {
#do what you want after keypress
}
>> stty will not work. stdin is a pipe and I just
>> can't turn of the line buffering.
> Now if you can put your fingers inside the guts of the writer, a
> frequent mistake is a lack of flushing.
Ah, ok.
The writer is my own very first erlang program. I can't explain why it
wouldn't send, but I'll investigate further into that direction.
Stupid me, I tested with 'cat <<eof | ./simple.tcl' and forgot that now
cat will do the line buffering...
Thanks also for the second answer.