Keypress event in ruby

N

Nikita Kuznetsov

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
 
N

Nikita Kuznetsov

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
 
R

Rahul Kumar

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

Rahul Kumar

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
 
N

Nikita Kuznetsov

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

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top