How do I scan for keypresses in console ruby?

I’m writing some text apps that need keyboard scanning… the only
module I’ve seen that does keypress scanning in Ruby is SDL, but that
sucks in X if I try to install it (Gentoo)… I don’t even have X
installed. Is there a module that does keyboard scanning in Ruby that
isn’t oriented toward X?

Thanks

Myrddin

“Myrddin Emrys” [email protected] writes:

I’m writing some text apps that need keyboard scanning… the only
module I’ve seen that does keypress scanning in Ruby is SDL, but that
sucks in X if I try to install it (Gentoo)… I don’t even have X
installed. Is there a module that does keyboard scanning in Ruby that
isn’t oriented toward X?

Check out cbreak.rb that is shipped with ruby as a sample program.

This doesn’t do quite what you want, but if you take the three
functions from that file, then you can play around with this code:

cbreak
while true do
if select( [$stdin], nil, nil, 10 ) then
puts "saw character ", $stdin.read(1)[0]
else
puts “Ten seconds elapsed with no input”
end
end
cooked

Note: Likely not to work on windows. In fact, it may not even work
anywhere, although it probably will if you make sure that the values
for the TIOCGETP and TIOCSETP constants match your system’s headers.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs