How to capture a keypress

S

Simon Blanco

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!
 
F

fedzor

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.
 
S

Simon Blanco

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 :D
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
(http://www.h3rald.com/blog/inline-introduction):


#!/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?
 
S

Simon Blanco

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

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 :(

Cheers and have fun

Sai
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top