I have a large TCL script that, at points asks the user questions like, "Please enter your choice : A, B or C ?"
The user has to type in the character and press "Enter" to continue to script. Is there anyway I can automate this in TCL? Something like, if the user doesn't enter anything within 10 seconds, by default option A will be taken and the script will continue?
I've looked around and it seems that the only way TCL accepts inputs is with the "gets" command - but this is blocking and won't proceed until the user enters something. Is there anything else I can use?
ANSWER:
Colin's reply is what ultimately led me to the answer I was looking for (along with a bit of googling). To those interested here is the code I ended up using,
puts "Are you happy right now?"
puts "\(Press \"n\" or \"N\" within 5 seconds if you say \"No\"\)\nANSWER:"
global GetsInput
exec /bin/stty raw <@stdin
set id [after 5000 {set GetsInput "Y"}]
fileevent stdin readable {set GetsInput [read stdin 1]}
vwait ::GetsInput
after cancel $id
set raw_data $GetsInput
exec /bin/stty -raw <@stdin
uplevel #0 "unset GetsInput"
set user_input [string toupper $raw_data]
puts "\n"
if [ string equal $user_input "N"] {
puts "You are making me upset as well!!\n"
} else {
puts "I'm happy for you too !! (^_^)\n"
}
unset raw_data user_input
What the above does is ask a question and wait for 5 seconds for a user key-press. It will accept ONLY 1 key as input (user doesn't need to press enter). It then prints out a response. The if statement is just to demonstrate how one could make decisions based on the above code. For better or worse, it won't run without "stty" support from your operating system.