From: James Gray Date: 2008-01-17T22:28:14+09:00 Subject: Re: Quit, if a specific key is hit On Jan 16, 2008, at 11:43 PM, Pavankumar Kulkarni wrote: > Excuse me, if this is already discussed... but from from here >.. 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 Gray II