[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Trapping ESC key

Harry

3/16/2007 10:57:00 AM

>
> I am stuck and can't find a simple and portable way to trap the ESC key. Any
> pointers, help is most appreciated.
>
>
Will this do it?

http://www.ruby-forum.com/topic/85...

http://blog.grayproductions.net/articles/2006/10/01/i-just-want-one...

Harry

--

http://www.kakueki.com/ruby...
Japanese Ruby List Subjects in English

1 Answer

Pit Capitain

3/16/2007 2:00:00 PM

0

RubyCrazy schrieb:
> Harry, I have tried HighLine but that's not what I want (or am I making a
> mistake here?). I am trying to solve the problem - ** "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)."
>
> Somehow I want to keep checking in a thread (this should be non-blocking)
> for the ESC key press. Once ESC is pressed my program terminates. Right
> now,
> my thread code blocks till I press ESC key, as you can see here -
>
> require 'Win32API'
> def getchar
> char = Win32API.new("crtdll", "_getch", [], 'L').call
> if char == 27 # ESC key
> exit
> end
> end
>
> i = 0
> loop do
> print "#{i+=1}, "
> getchar
> end
>
> What should I do? Thanks.

Change your operating system, or don't use threads with blocking
keyboard I/O. It doesn't work on Windows. For your code above (which
doesn't use threads) you could call the Win32 function "_kbhit" before
calling "_getch". In a thread you could do the same in a busy loop, but
don't forget to sleep a little bit inside the loop.

Regards,
Pit