How do I scan for keypresses in console ruby?

M

Myrddin Emrys

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
 
D

Daniel Martin

Myrddin Emrys said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top