# How do I scan for keypresses in console ruby?

**URL:** <https://www.ruby-forum.com/t/how-do-i-scan-for-keypresses-in-console-ruby/99663>\
**Category:** Ruby\
**Created:** [July 24, 2006, 7:33pm UTC](https://www.ruby-forum.com/t/how-do-i-scan-for-keypresses-in-console-ruby/99663 "2006-07-24T19:33:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Myrddin\_Emrys](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c2a13f/32.png) [@Myrddin\_Emrys](https://www.ruby-forum.com/u/Myrddin_Emrys)\
**Post date:** [July 24, 2006, 7:33pm UTC](https://www.ruby-forum.com/t/how-do-i-scan-for-keypresses-in-console-ruby/99663/1 "2006-07-24T19:33:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Myrddin\_Emrys](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c2a13f/32.png) [@Myrddin\_Emrys](https://www.ruby-forum.com/u/Myrddin_Emrys)\
**Post date:** [July 24, 2006, 8:05pm UTC](https://www.ruby-forum.com/t/how-do-i-scan-for-keypresses-in-console-ruby/99663/2 "2006-07-24T20:05:51Z")

</div>

“Myrddin Emrys” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.
