Trapping keystrokes

T

Thomas Aaron

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
 
V

Vincent Fourmond

Thomas said:
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
 
T

Thomas Aaron

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.
 

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
474,219
Messages
2,571,118
Members
47,731
Latest member
unutbu

Latest Threads

Top