Reputation: 11493
I am writing a tiny interactive command line tool prompting the user to press a numeric key. It should continue directly after the first key press/input.
Currently I am doing this to capture the user's input
puts "yes, please ..."
gets.chomp
... however this requires pressing "enter" to confirm the input. How can return the input value right after the first key press?
Upvotes: 1
Views: 700
Reputation: 54684
Try something like this:
puts 'Do you want to proceed? y/n'
loop do
system("stty raw -echo")
c = STDIN.getc
system("stty -raw echo")
case c
when 'y'
puts 'Yes'
break
when 'n'
puts 'No'
break
else
puts 'Please type "y" or "n"'
end
end
Upvotes: 1