# Quit, if a specific key is hit

**URL:** <https://www.ruby-forum.com/t/quit-if-a-specific-key-is-hit/126301>\
**Category:** Ruby\
**Created:** [January 17, 2008, 6:43am UTC](https://www.ruby-forum.com/t/quit-if-a-specific-key-is-hit/126301 "2008-01-17T06:43:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![pavan1919](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/cc9497/32.png) [@pavan1919](https://www.ruby-forum.com/u/pavan1919)\
**Post date:** [January 17, 2008, 6:43am UTC](https://www.ruby-forum.com/t/quit-if-a-specific-key-is-hit/126301/1 "2008-01-17T06:43:50Z")

</div>

Excuse me, if this is already discussed… but from from here  
[http://www.rubyrailways.com/implementing-15-exercises-for-learning-a-new-programming-language/](http://www.rubyrailways.com/implementing-15-exercises-for-learning-a-new-programming-language/)…  
the author concludes that the following problem cannot be solved… whats  
the reason?  
“Display series of numbers (1,2,3,4, 5…etc) in an infinite loop. The  
program should quit if someone hits a specific key (Say ESCAPE key).”

---

<div class="post-metadata">

**Author:** ![pavan1919](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/cc9497/32.png) [@pavan1919](https://www.ruby-forum.com/u/pavan1919)\
**Post date:** [January 17, 2008, 2:29pm UTC](https://www.ruby-forum.com/t/quit-if-a-specific-key-is-hit/126301/2 "2008-01-17T14:29:34Z")

</div>

On Jan 16, 2008, at 11:43 PM, Pavankumar Kulkarni wrote:

> Excuse me, if this is already discussed… but from from here \<[http://www.rubyrailways.com/implementing-15-exercises-for-learning-a-new-programming-language/](http://www.rubyrailways.com/implementing-15-exercises-for-learning-a-new-programming-language/)
> 
> > … the author concludes that the following problem cannot be  
> > solved… whats the reason?  
> > “Display series of numbers (1,2,3,4, 5…etc) in an infinite loop.  
> > The program should quit if someone hits a specific key (Say ESCAPE  
> > key).”

It can definitely be solved. Here’s a solution that works on most  
Unix-like operating systems, for example:

#!/usr/bin/env ruby -wKU

require “io/wait”

state = `stty -g`  
begin  
system “stty raw -echo cbreak isig”

```
 1.upto(1.0/0.0) do |n|
   puts n
   exit if $stdin.ready? and $stdin.getc == 27
 end

```

ensure  
system “stty #{state}”  
end

**END**

What the author actually said was that it can’t solve it without  
writing some platform specific code or threads. I don’t know how to  
do it without using one of those tricks either.

The reason is that all terminals are different and you are needing to  
interact with it on two levels at once (reading while writing). This  
is what introduces the need for the platform specific code.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![pavan1919](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/cc9497/32.png) [@pavan1919](https://www.ruby-forum.com/u/pavan1919)\
**Post date:** [January 18, 2008, 2:06am UTC](https://www.ruby-forum.com/t/quit-if-a-specific-key-is-hit/126301/3 "2008-01-18T02:06:35Z")

</div>

On Jan 17, 2008, at 7:28 AM, James G. wrote:

> It can definitely be solved. Here’s a solution that works on most  
> 1.upto(1.0/0.0) do |n|  
> What the author actually said was that it can’t solve it without  
> writing some platform specific code or threads. I don’t know how  
> to do it without using one of those tricks either.
> 
> The reason is that all terminals are different and you are needing  
> to interact with it on two levels at once (reading while writing).  
> This is what introduces the need for the platform specific code.
> 
> James Edward G. II

In many of the game libraries, input for different platforms is  
addressed.  
This one of those cases where the game libraries, such as Gosu or  
rubygame, or Ruby/SDL are going to be very useful for a non game.  
most game libs have a very fundamental need for input control, and a  
pretty important need for crossplatform code abstraction.  
You don’t need to know much about them, just enough to get the keys  
under your control!  
But the plus is, you can also get a GUI up without much work.

---

<div class="post-metadata">

**Author:** ![pavan1919](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/cc9497/32.png) [@pavan1919](https://www.ruby-forum.com/u/pavan1919)\
**Post date:** [January 18, 2008, 5:11am UTC](https://www.ruby-forum.com/t/quit-if-a-specific-key-is-hit/126301/4 "2008-01-18T05:11:58Z")

</div>

Cool… that clears some air!.. Cheers guys!!..  
Hail Ruby Community!! 🙂

–  
Pavan  
Software Developer  
Persistent Systems Ltd, Goa, India

---

<div class="post-metadata">

**Author:** ![pavan1919](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/cc9497/32.png) [@pavan1919](https://www.ruby-forum.com/u/pavan1919)\
**Post date:** [January 18, 2008, 11:20am UTC](https://www.ruby-forum.com/t/quit-if-a-specific-key-is-hit/126301/5 "2008-01-18T11:20:48Z")

</div>

I found a platform independent solution to this in one of the comments  
of that link(below) here  
[http://www.rubyrailways.com/implementing-15-exercises-for-learning-a-new-programming-language/#comment-1145](http://www.rubyrailways.com/implementing-15-exercises-for-learning-a-new-programming-language/#comment-1145).

i=0  
loop do  
begin  
break if STDIN.read\_nonblock(1000)  
rescue Errno::EAGAIN  
end  
puts i  
i += 1  
end

Cheers,  
Pavankumar Kulkarni \* Software Developer \* Persistent Systems Ltd

---

<div class="post-metadata">

**Author:** ![pavan1919](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/cc9497/32.png) [@pavan1919](https://www.ruby-forum.com/u/pavan1919)\
**Post date:** [January 18, 2008, 5:17pm UTC](https://www.ruby-forum.com/t/quit-if-a-specific-key-is-hit/126301/6 "2008-01-18T17:17:41Z")

</div>

On Jan 18, 2008 4:19 AM, Pavankumar Kulkarni wrote:

> I found a platform independent solution to this in one of the comments  
> of that link(below) here  
> [http://www.rubyrailways.com/implementing-15-exercises-for-learning-a-new-programming-language/#comment-1145](http://www.rubyrailways.com/implementing-15-exercises-for-learning-a-new-programming-language/#comment-1145).  
> rescue Errno::EAGAIN

I could be wrong, but I was under the impression that _all_ Errnos  
were platform-dependant.

Daniel Brumbaugh K.

---

<div class="post-metadata">

**Author:** ![pavan1919](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/cc9497/32.png) [@pavan1919](https://www.ruby-forum.com/u/pavan1919)\
**Post date:** [January 18, 2008, 5:45pm UTC](https://www.ruby-forum.com/t/quit-if-a-specific-key-is-hit/126301/7 "2008-01-18T17:45:54Z")

</div>

On Jan 18, 2008 4:19 AM, Pavankumar Kulkarni  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I found a platform independent solution to this in one of the comments  
> of that link(below) here  
> [http://www.rubyrailways.com/implementing-15-exercises-for-learning-a-new-programming-language/#comment-1145](http://www.rubyrailways.com/implementing-15-exercises-for-learning-a-new-programming-language/#comment-1145).

That does not fulfill the stated requirements, does it?

> i=0  
> loop do  
> begin  
> break if STDIN.read\_nonblock(1000)

C defines stdin to be buffered, so characters will pile up in the  
buffer until an EOL is seen or the buffer is full. It might be the  
case that some platform does not do this, but in general the program  
won’t stop until you press a lot of keys or (usually), press enter.

Marcelo
