Keypress event in ruby

Hi guys, im working on making a little tetris program in ruby (console
based, no gui). My ruby knowledge isnt great but i would love to get
more familiar with the language, i kinda like the scripting side to it.

So my question is, how do you hand keypress events. For example i got a
shape falling down (lets say just a . for the point being).
Lets say i run a for loop to drop the shape, onKeyPressA, move shape
left 1 square.
how would i implement the onKeyPress part, what libraries should i use?
Any help would be appreciated. I havent written much code yet so i'm
figuring posting it here would be kinda useless, all i need is to know
how to create methods for keypress event handling.

Thanks in advance,
Nikita

···

--
Posted via http://www.ruby-forum.com/.

look into a library called ncurses

···

~

On Fri, Nov 26, 2010 at 2:57 AM, Nikita Kuznetsov <moog_master@hotmail.com> wrote:

Hi guys, im working on making a little tetris program in ruby (console
based, no gui). My ruby knowledge isnt great but i would love to get
more familiar with the language, i kinda like the scripting side to it.

So my question is, how do you hand keypress events. For example i got a
shape falling down (lets say just a . for the point being).
Lets say i run a for loop to drop the shape, onKeyPressA, move shape
left 1 square.
how would i implement the onKeyPress part, what libraries should i use?
Any help would be appreciated. I havent written much code yet so i'm
figuring posting it here would be kinda useless, all i need is to know
how to create methods for keypress event handling.

Thanks in advance,
Nikita

--
Posted via http://www.ruby-forum.com/.

Uum, that doesn't really answer my question, well it does.. but i would
look at that answer as a part 2 of my exercise... As i understood, the
ncurses library lets you create guis, i dont want to create one at
first. I would like to get more familiarised with ruby. and so i would
be deeply appreciative if some1 would show some function something like:

something like
if myEvent.keyPress()==A
    do something
end

what i'm stuck at is, how would i create my own event class(i'm kind of
guessing i will have to create an event class here) and i would like to
make my own, that way i write the code and get more of the feel.

class Event

def keyPress()
#help me here please :)))
end

···

--
Posted via http://www.ruby-forum.com/.

Ah great, i guess i will stick to ncurses then. Thanks for the help guys
Thats what i was looking for

···

--
Posted via http://www.ruby-forum.com/.

Nikita Kuznetsov wrote in post #964064:

Uum, that doesn't really answer my question, well it does..

ncurses also allows you to trap keystrokes, function keys, control and
alt key combinations etc.

IIRC, once you create a Window, you can do getch() on the window, and
accept
characters from the user. Once you have trapped the key (its very
simple) then you can dispatch to the method you wish to.

I suggest you install ncurses, and check out the samples. It will be
very clear.

···

--
Posted via http://www.ruby-forum.com/.

Nikita Kuznetsov wrote in post #964064:

def keyPress()
#help me here please :)))
end

I've cut this from a sample that comes with ncurses.

  while((ch = win.getch()) != Ncurses::KEY_F1) do
  case(ch)
    when Ncurses::KEY_DOWN
      go_down()

    when Ncurses::KEY_UP
      go_up()

    when ...

    else
  end
    paint_screen()
  end

# win is a window

···

--
Posted via http://www.ruby-forum.com/.