[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Trapping keystrokes

Thomas Aaron

11/28/2006 8:49:00 PM

Hi, guys.
I'm a Python guy who is considering switching to Ruby.

Quick question:

In python in order to trap individual keystrokes in text mode, you have
to import the curses library and write a program that initializes a
window in your terminal...blah.blah.blah.

What I really need is just a simple command that captures a single
keystroke. Kinda like the old BASIC command: inkey$

Python does not seem to have an equivalent (other than all of the curses
rigamorole).

Does Ruby have a command that does this?

Thank you.
Best,
Tom

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

3 Answers

Vincent Fourmond

11/28/2006 11:13:00 PM

0

Thomas Aaron wrote:
> Hi, guys.
> Python does not seem to have an equivalent (other than all of the curses
> rigamorole).
>
> Does Ruby have a command that does this?

That doesn't exist. The reason is that it is terribly os-dependent.
That's why you need all the curses blunder, and even this is not
satisfying, as it won't run under win32.

Sorry...

Vince

--
Vincent Fourmond, PhD student
http://vincent.fourmon...

James Gray

11/28/2006 11:19:00 PM

0

On Nov 28, 2006, at 2:48 PM, Thomas Aaron wrote:

> In python in order to trap individual keystrokes in text mode, you
> have
> to import the curses library and write a program that initializes a
> window in your terminal...blah.blah.blah.

> Does Ruby have a command that does this?

http://blog.grayproductions.net/articles/2006/10/01/i-just...
character

Hope that helps.

James Edward Gray II

Thomas Aaron

11/28/2006 11:24:00 PM

0

> That doesn't exist. The reason is that it is terribly os-dependent.
> That's why you need all the curses blunder, and even this is not
> satisfying, as it won't run under win32.
>
> Sorry...
>
> Vince

Ahhh! That makes sense. I'm sure that's why Python doesn't have it as
well.
Thanks, Vince.

In case anyone else stumbles across this post in search of something
similar, below is a script that I figured out for detecting keystrokes
in BASH. I think it will fit my purposes and may hep someone else, too.

#!/bin/bash
# Capture keystrokes without needing to press ENTER.
old_tty_setting=$(stty -g) # Save old terminal settings.
stty -icanon -echo # Disable canonical mode and echo
keys=$(dd bs=1 count=1 2> /dev/null)
echo you pressed $keys
stty "$old_tty_setting" # Restore old terminal settings.
exit 0

With a little research, you can create a menu-driven,
keystroke-detecting program.


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