Problem Having Ruby Wait for a User Input

I am new to Ruby an have what should be a simple problem. I want to create
code that first accepts a user input from the keyboard, and then continues
to execute code. I am capable of writing a method that registers input
from a keyboard a stores them within variables for my needs. However,
this methods executes so fast that unless a key was being pressed at the
time it was called, it is impossible for the user the press the key in
time.

Currently, my only need is to collect an input from the keyboard, I am not
interested in running any other methods concurrently while listening for
user input. I just want to program to wait, and not act in any way, until
an input from the keyboard is received.

As a side note, is there anything similar to the Java "listeners" that I
can use in Ruby.

Thanks for your help.

Does this do what you want?

print "What is your name "
name = readline
print "Hello, #{name}\n"

Or are you wanting to wait for a single keystroke? If so, what OS are
you using? AFAIK, there is no good operating system independent way to
do this, but there are good ways for any given OS.

-- Markus

···

On Sun, 2004-09-05 at 20:35, Phanixis wrote:

I am new to Ruby an have what should be a simple problem. I want to create
code that first accepts a user input from the keyboard, and then continues
to execute code. I am capable of writing a method that registers input
from a keyboard a stores them within variables for my needs. However,
this methods executes so fast that unless a key was being pressed at the
time it was called, it is impossible for the user the press the key in
time.

Currently, my only need is to collect an input from the keyboard, I am not
interested in running any other methods concurrently while listening for
user input. I just want to program to wait, and not act in any way, until
an input from the keyboard is received.

As a side note, is there anything similar to the Java "listeners" that I
can use in Ruby.

Thanks for your help.

Is just want a single keystroke. The operating system I am using is
Windows XP, but that should not have anything to do with the problem.

The code that I list is well capable of registering a key stroke. If a
key is being press, and the method I list is invoked, it will register the
key press(provided it is one of the keys its designed for). It is not so
much the registering of the key stroke a need, as the WAIT UNTIL a key is
pressed. I want to take the existing code a created, and make it sit
there and wait for a key stroke.

Ordinarily, I would do this simply by placing the code inside a while
loop, and once the code registered a key input, it would change a variable
and terminate the while loop. But for whatever reason, once nested in the
loop, the method ceases to register key strokes.

According to "The Ruby Way", the Windows method for reading a single keystroke is:

require "Win32API"
char = Win32API.new("crtdll", "_getch", , "L").Call

I assume you could wrap that call in a loop, checking for the keys you want.

Hope that helps.

James Edward Gray II

···

On Sep 5, 2004, at 11:40 PM, Phanixis wrote:

Is just want a single keystroke. The operating system I am using is
Windows XP, but that should not have anything to do with the problem.