# Trapping keystrokes

**URL:** <https://www.ruby-forum.com/t/trapping-keystrokes/77569>\
**Category:** Ruby\
**Created:** [November 28, 2006, 9:48pm UTC](https://www.ruby-forum.com/t/trapping-keystrokes/77569 "2006-11-28T21:48:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomuhs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecb155/32.png) [@tomuhs](https://www.ruby-forum.com/u/tomuhs)\
**Post date:** [November 28, 2006, 9:48pm UTC](https://www.ruby-forum.com/t/trapping-keystrokes/77569/1 "2006-11-28T21:48:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tomuhs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecb155/32.png) [@tomuhs](https://www.ruby-forum.com/u/tomuhs)\
**Post date:** [November 29, 2006, 12:13am UTC](https://www.ruby-forum.com/t/trapping-keystrokes/77569/2 "2006-11-29T00:13:08Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![tomuhs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecb155/32.png) [@tomuhs](https://www.ruby-forum.com/u/tomuhs)\
**Post date:** [November 29, 2006, 12:21am UTC](https://www.ruby-forum.com/t/trapping-keystrokes/77569/3 "2006-11-29T00:21:09Z")

</div>

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-want-one-](http://blog.grayproductions.net/articles/2006/10/01/i-just-want-one-)  
character

Hope that helps.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![tomuhs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecb155/32.png) [@tomuhs](https://www.ruby-forum.com/u/tomuhs)\
**Post date:** [November 29, 2006, 12:23am UTC](https://www.ruby-forum.com/t/trapping-keystrokes/77569/4 "2006-11-29T00:23:44Z")

</div>

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