# Sensing keyboard and mouse?

**URL:** <https://www.ruby-forum.com/t/sensing-keyboard-and-mouse/112614>\
**Category:** Ruby\
**Created:** [August 21, 2007, 1:06pm UTC](https://www.ruby-forum.com/t/sensing-keyboard-and-mouse/112614 "2007-08-21T13:06:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![arun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ee7513/32.png) [@arun](https://www.ruby-forum.com/u/arun)\
**Post date:** [August 21, 2007, 1:06pm UTC](https://www.ruby-forum.com/t/sensing-keyboard-and-mouse/112614/1 "2007-08-21T13:06:18Z")

</div>

I want to interrupt something, when the user presses a key, or clicks  
the mouse. The only solution for the keyboard I could come with, seems  
overkill:

a=0  
th=Thread.new {loop{a=STDIN.getc}}  
loop do  
a=0  
sleep (1)  
if a==0  
puts “nothing pressed”  
else  
puts “key pressed”  
end  
end

Also, it requires that I press the Enter key… I would be very  
grateful, if you could show me a better way.

Arun

---

<div class="post-metadata">

**Author:** ![arun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ee7513/32.png) [@arun](https://www.ruby-forum.com/u/arun)\
**Post date:** [August 21, 2007, 6:12pm UTC](https://www.ruby-forum.com/t/sensing-keyboard-and-mouse/112614/2 "2007-08-21T18:12:11Z")

</div>

On Aug 21, 2007, at 7:05 AM, arun wrote:

> if a==0  
> puts “nothing pressed”  
> else  
> puts “key pressed”  
> end  
> end
> 
> Also, it requires that I press the Enter key… I would be very  
> grateful, if you could show me a better way.
> 
> Arun

Check out the termios library - and the HighLine gem.

Sorry if it’s brief, I have to run!  
Ari  
-------------------------------------------|  
Nietzsche is my copilot
