keytclpressed

tcl pause & waiting for key pressed to continue


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.


Solution

  • 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