Pit Capitain
3/16/2007 2:00:00 PM
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