2

I know about "a = gets", but I need to read characters from keyboard without pressing enter. How can I do this in ruby?

DarkSun
  • 451
  • 1
  • 7
  • 19

2 Answers2

1

You can try the STDIN, please check its document. It's a powerful tool to control your standard input .

staticor
  • 620
  • 9
  • 19
1

use the green_shoes gem or simply use red shoes, here a green_shoes working sample

 ['green_shoes'].each(&method(:require))
Shoes.app do
 e = edit_line
 info = para "NO KEY is PRESSED."
 keypress do |k|
   info.replace "#{k.inspect} was PRESSED."
   print k
 end
end

Works on any OS unlike the sollution from Detect key press (non-blocking) w/o getc/gets in Ruby Put your shoes on !

Community
  • 1
  • 1
peter
  • 41,770
  • 5
  • 64
  • 108