How to capture a keypress

Hi there,
I’m making an interface for a console program that should react whenever
the user presses the keys a, m, n or s.
I tried to look for a method like keydown or keypress, but I couldn’t
find anything alike, neither any gem related to that purpose (I’m still
newbie using rubygems and to ruby either ^^" )

It would be great if someone could point me to any method or library,
where to download it, and any info to use it (or if it can be found at
ri)

thanks in advance!

On Tue, Jun 17, 2008 at 9:10 AM, Simon B. [email protected]
wrote:

Hi there,
I’m making an interface for a console program that should react whenever
the user presses the keys a, m, n or s.

I have not tried it, but you may want to take a look at this.

http://blog.grayproductions.net/articles/i_just_want_one_character

Harry

On Jun 16, 2008, at 8:10 PM, Simon B. wrote:

Hi there,
I’m making an interface for a console program that should react
whenever
the user presses the keys a, m, n or s.
I tried to look for a method like keydown or keypress, but I couldn’t
find anything alike, neither any gem related to that purpose (I’m
still
newbie using rubygems and to ruby either ^^" )

Check out rawline, and learn to use that!
http://rubyforge.org/projects/rawline/

the examples are pretty good.

_______________________________|

  • Ari
    I just bought another case of RockStar. Expect architectural changes.

I have not tried it, but you may want to take a look at this.

http://blog.grayproductions.net/articles/i_just_want_one_character

Harry

Sorry for the delay firstly. I’ve been busy lately :smiley:
I’ve read over the documentation of Highline and didn’t find what I
needed, but after it I checked out the rawline gem and I found a great
example at the author’s web
(Home - H3RALD):

#!/usr/local/bin/ruby -w

require ‘rubygems’
require ‘highline/system_extensions’

include HighLine::SystemExtensions

puts “Press a key to view the corresponding ASCII code(s) (or CTRL-X to
exit).”

loop do

print "=> "
char = get_character
case char
when ?\C-x: print "Exiting..."; exit;
else puts "#{char.chr} [#{char}] (hex: #{char.to_s(16)})";
end

end

…so I can use highline finally (thanks! :D)

I just don’t understand one thing from the code above: the “?\C-x”, in
the when statement. Can anybody tell me what does it mean?

From: [email protected] [mailto:[email protected]]
#…

I just don’t understand one thing from the code above: the

“?\C-x”, in the when statement.

if you studied the code, you can deduce that it’s a literal character
for the keyboard combination Control-x. In ruby, single characters can
be represented by prefixing the ? to the char but without the usual
quoting.

There are some caveats however, and the best way to learn/test is using
irb, eg

RUBY_VERSION
#=> “1.8.6”

?\C-x
#=> 24

so in ruby1.8.6, the single char evaluates to an integer, wc if you look
closely, is the ascii code. again, note, it’s an integer.

?\C-x.class
#=> Fixnum

of course you can represent single chars as a string,

“\C-x”
#=> “\030”

oops, you’ll have to evaluate that further to get to the ? behaviour

030
#=> 24

ergo, you cannot compare ?-ied chars with quoted chars directly.

?\C-x == “\C-x”
#=> false

again,

?a
#=> 97

“a”
#=> “a”

?a == “a”
#=> false

:slight_smile:

also, judging fr the above, you cannot represent multi-byte chars using
the ? in ruby 1.8.6.

In ruby1.9 however, it’s much better and more natural. the way it should
be, so to speak.

?\C-x
=> “\x18”

“\C-x”
=> “\x18”

?a
=> “a”

“a”
=> “a”

?a == “a”
=> true

Ergo, in ruby1.9, you may not need the ? single char representation, the
usual quoting may do.

kind regards -botp

Glad to hear you found my example of use!

Chers,

Fabio

woah, I’ll save this post in case I need to remind about getting ascii
code and working with keyb :smiley:

Posted by h3raLd (Guest)
on 19.06.2008 23:25
(Received via mailing list)

Glad to hear you found my example of use!

Chers,

Fabio

I just had to make few changes to your code to make the menu of my app
work flawlessly, so really thanks to everyone.

Sadly I’ll have to learn COSMOS next weeks for job, so I guess I won’t
have too much time to play around with ruby :frowning:

Cheers and have fun

Sai