Trapping keystrokes

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-forum.com/.

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.fourmond.neuf.fr/

Hope that helps.

James Edward Gray II

···

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?

  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-forum.com/\.