Problem Having Ruby Wait for a User Input

Oh, your first link is broken.

Both links work - just retested. If you have trouble accessing it go to
www.ruby-doc.org, Core API, and select "lib/observer.rb" from under
"Files".

Here is the code in question, it is intended to get only a single key

press

for a limited range of keys.

def key_input #Get arbitrary keyboard input
   input = 0
   n = 0
   for i in 1..18
     if Input.trigger?(i)
       n = i
     end
   end
   if n > 0
     input = n
   end
  return input
end

Does this clear anything up?

There is no code above to get a character from std-in. If you want
single characters from a stream you can use IO#getc, as in:
  a = STDIN.getc
Or:
  a = STDIN.sysread(1)

But both depend on the underlying OS buffering - i.e. you will not get
the characters until the user feed the input line by presseing enter.

Thus my question about what OS you are running. Under linux (for
example) you can get around the buffering by writing something like:

begin
    `stty raw -echo`

···

On Sun, 2004-09-05 at 21:33, Mehr, Assaph (Assaph) wrote:

But both depend on the underlying OS buffering - i.e. you will not get
the characters until the user feed the input line by presseing enter.

     #
     # your code goes here...
     #
  ensure
    `stty -raw echo`
  end

-- Markus

The code above is capable of acquiring keyboard inputs, at least within the
application that I am using it for. The problem that I am having is
getting the code to wait for the user input, by doing something like
nesting it in a loop that does not terminate until the variable input is
nonzero(but that doesn't seem to work). Like I said before, if the method
above is run while the appropriate key is being pressed, it will record
the value in the variable input. I just need it to register key inputs
over a longer interval of time.

Thanks for all the help so far.

Also,the first link is still not working for me.

Thanks.

The thing is, I am quite happy with the code I have already devised for
registering keyboard inputs. It works, at least to a degree, registering
single key strokes without a problem. Thus, its not so much a question of
how to register the keystrokes as it is to get the code that does register
the keystrokes to wait for those keystrokes. The solution should be
related to loops or commands that will give the user time to enter the
keystrokes, as opposed to new methods of registering them.

In any case, thanks for all of the help so far.

Hi,

The thing is, I am quite happy with the code I have already devised for
registering keyboard inputs. It works, at least to a degree, registering
single key strokes without a problem.

I'm not trying to be a pain - but .... If your current code
stops working mysteriously when used inside a loop, it sounds
like at least one giant reason not to be entirely quite happy
with it. :slight_smile:

In any case,

def key_input #Get arbitrary keyboard input
    input = 0
    n = 0
    for i in 1..18
      if Input.trigger?(i)
        n = i
      end
    end
    if n > 0
      input = n
    end
   return input
  end

The above just sets "input" equal to a number between
1 and 18, right?

If you need to make that loop last longer, you could
try adding a sleep() . . . I'm only suggesting this
because I have no idea what you're trying to do,
really. But for example:

     for i in 1..18
       if Input.trigger?(i)
         n = i
         break # added
       end
       sleep(0.1) # sleep 1/10 sec
     end

How is Input.trigger? itself implemented ?

Regards,

Bill

···

From: "Phanixis" <phanixis@yahoo.com>