[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How do I scan for keypresses in console ruby?

Myrddin Emrys

7/24/2006 5:28:00 PM

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

1 Answer

Daniel Martin

7/24/2006 6:05:00 PM

0

"Myrddin Emrys" <myrddin@gmail.com> 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.