tcl pause & waiting for key pressed to continue
Asked Answered
B

3

6

I'm looking for a way in tcl to pause the script (for example after some outputs with "puts") and wait for a key pressed from the user before continuing to output the remaining text.

Barrage answered 24/9, 2013 at 22:22 Comment(1)
Are you sure that you want to build that into the script? Someone might want to send the output to a file or pipe it to grep, sed or whatever. A better solution is to pipe the scripts output to one of the programs more (available on Windows too) or less.Masticatory
H
5

With gratitude to Hai Vu's answer, if you're on a unix-like system with stty

proc anykey {{msg "Hit any key: "}} {
    set stty_settings [exec stty -g]
    exec stty raw -echo
    puts -nonewline $msg
    flush stdout
    read stdin 1
    exec stty $stty_settings
    puts ""
}
puts 1
anykey
puts 2

Link to thorough discussion on Tcl wiki: Reading a single character from the keyboard using Tcl

Happygolucky answered 25/9, 2013 at 11:17 Comment(2)
Doing the same thing on Windows requires using the TWAPI package so that you can change the terminal mode. It's a little long for a comment.Copious
Extra note: if you want to a key this way more than once, stty -icanon has the desired effect, but is not a severe as stty raw - e.g. it lets the user quit using ^C as usual.Villenage
A
16

You just use gets to read from stdin:

proc pause {{message "Hit Enter to continue ==> "}} {
    puts -nonewline $message
    flush stdout
    gets stdin
}

pause "Hurry, hit enter: "; # Sample usage 1
pause;                      # Sample usage 2, use default message
Addressograph answered 25/9, 2013 at 1:7 Comment(2)
+1 for your Answer. Beautiful code provided. After seeing your answer, 1 question came to my mind.. What if i want user to "press any key"? not just "Enter"... Is such thing possible? I would have started this question as a new thread, but was not confident for such scenario.. Is there any option/way?Deportment
thank you for this short code, it works very well with enter key.Barrage
H
5

With gratitude to Hai Vu's answer, if you're on a unix-like system with stty

proc anykey {{msg "Hit any key: "}} {
    set stty_settings [exec stty -g]
    exec stty raw -echo
    puts -nonewline $msg
    flush stdout
    read stdin 1
    exec stty $stty_settings
    puts ""
}
puts 1
anykey
puts 2

Link to thorough discussion on Tcl wiki: Reading a single character from the keyboard using Tcl

Happygolucky answered 25/9, 2013 at 11:17 Comment(2)
Doing the same thing on Windows requires using the TWAPI package so that you can change the terminal mode. It's a little long for a comment.Copious
Extra note: if you want to a key this way more than once, stty -icanon has the desired effect, but is not a severe as stty raw - e.g. it lets the user quit using ^C as usual.Villenage
A
0

A potential solution for Windows:

exec -ignorestderr [file join $env(windir) system32 cmd.exe] /c pause

This notices non-ascii keypresses, too (such as arrow keys, Escape etc.).

Another solution is the Raw Mode on Windows proposed by Donal Fellows above (thanks for the link!), which can be summarized into these two lines of code:

twapi::modify_console_input_mode stdin -lineinput 0 -echoinput 0
read stdin 1

This does not notice Escape, arrows keys and the like (and you may need to restore the console input mode later).

Aveyron answered 13/5, 2016 at 12:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.