Forum: Ruby Trapping ESC key

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
I G (Guest)
on 2007-03-16 09:10
(Received via mailing list)
I am a Ruby newbie and am trying to implement problem 1 mentioned here -
http://www.rubyrailways.com/implementing-15-exerci...


I am stuck and can't find a simple and portable way to trap the ESC key.
Any
pointers, help is most appreciated. The code I have now is -

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

Thanks.
Harry H. (Guest)
on 2007-03-16 12:57
(Received via mailing list)
>
> 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/85036#156718

http://blog.grayproductions.net/articles/2006/10/0...

Harry

--

http://www.kakueki.com/ruby/list.html
Japanese Ruby List Subjects in English
RubyCrazy (Guest)
on 2007-03-16 13:13
(Received via mailing list)
On 3/16/07, Harry <removed_email_address@domain.invalid> wrote:
> http://blog.grayproductions.net/articles/2006/10/0...
>
> Harry
>

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.
Pit C. (Guest)
on 2007-03-16 16:00
(Received via mailing list)
RubyCrazy schrieb:
> require 'Win32API'
>  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
Peña, Botp (Guest)
on 2007-03-17 06:22
(Received via mailing list)
From: I G [mailto:removed_email_address@domain.invalid] :
# I am stuck and can't find a simple and portable way to trap
# the ESC key. Any
# pointers, help is most appreciated. The code I have now is -
#
# 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

can you try this sample?

C:\family\ruby\key_press>cat -n a1d.rb
     1  #------------------------------
     2  require 'Win32API'
     3
     4  @@kbhit = Win32API.new("msvcrt", "_kbhit", [], 'I')
     5  @@getch = Win32API.new("msvcrt", "_getch", [], 'I')
     6
     7  def capture_key
     8     unless @@kbhit.call.zero?
     9        yield @@getch.call
    10     end
    11  end
    12
    13  KEY_Esc = 27
    14  $stdout.sync=true
    15
    16  i=0
    17  loop do
    18     capture_key do |key|
    19        puts "keypressed: value #{key}, char #{key.chr}" if $DEBUG
    20        if key == KEY_Esc
    21           puts "Escaping key pressed. Exiting..." if $DEBUG
    22           exit
    23        end
    24     end
    25     sleep 1  #delay it since it's too fast :)
    26     puts "#{i+=1}"
    27  end
    28  #------------------------------

C:\family\ruby\key_press>ruby -d a1d.rb
1
2
3
4
keypressed: value 117, char u
5
keypressed: value 101, char e
6
7
8
keypressed: value 119, char w
9
10
keypressed: value 120, char x
11
12
keypressed: value 27, char ?
Escaping key pressed. Exiting...

C:\family\ruby\key_press>

Hth.
kind regards -botp
This topic is locked and can not be replied to.