# Single character input without pressing 'Enter'

**URL:** <https://rubytalk.org/t/single-character-input-without-pressing-enter/31141>\
**Category:** ruby-talk\
**Created:** [20 September 2006 03:45 UTC](https://rubytalk.org/t/single-character-input-without-pressing-enter/31141 "2006-09-20T03:45:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![unni.tallman](https://avatars.discourse-cdn.com/v4/letter/u/f04885/32.png) [@unni.tallman](https://rubytalk.org/u/unni.tallman)\
**Post date:** [20 September 2006 03:45 UTC](https://rubytalk.org/t/single-character-input-without-pressing-enter/31141/1 "2006-09-20T03:45:05Z")

</div>

how can i read input from stdin, char by char, i want to get the  
character as soon as it is entered, without having to press 'Enter'  
after each character.

---

<div class="post-metadata">

**Author:** ![Nguyen\_Huu\_Bach](https://avatars.discourse-cdn.com/v4/letter/n/a698b9/32.png) [@Nguyen\_Huu\_Bach](https://rubytalk.org/u/Nguyen_Huu_Bach)\
**Post date:** [20 September 2006 03:55 UTC](https://rubytalk.org/t/single-character-input-without-pressing-enter/31141/2 "2006-09-20T03:55:06Z")

</div>

This is a simple way to achieve that. Maybe there are others

def read\_char  
system "stty raw -echo" STDIN.getc ensure system "stty -raw echo"  
end

good luck

unni.tallman wrote:

> **···**
>
> > how can i read input from stdin, char by char, i want to get the  
> > character as soon as it is entered, without having to press 'Enter'  
> > after each character.

---

<div class="post-metadata">

**Author:** ![E\_S](https://avatars.discourse-cdn.com/v4/letter/e/3be4f8/32.png) [@E\_S](https://rubytalk.org/u/E_S)\
**Post date:** [20 September 2006 04:33 UTC](https://rubytalk.org/t/single-character-input-without-pressing-enter/31141/3 "2006-09-20T04:33:45Z")

</div>

Nguyen Huu Bach wrote:

> This is a simple way to achieve that. Maybe there are others
> 
> def read\_char

&nbsp;&nbsp;&nbsp;# Save previous state  
&nbsp;&nbsp;&nbsp;old = `stty -g`

> system "stty raw -echo"  
> STDIN.getc  
> ensure

&nbsp;&nbsp;&nbsp;&nbsp;system "stty #{old}"

> end
> 
> good luck

Might be better to use Termios or Curses, though.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [20 September 2006 12:56 UTC](https://rubytalk.org/t/single-character-input-without-pressing-enter/31141/4 "2006-09-20T12:56:17Z")

</div>

Or use HighLine which handles all these details for you.

James Edward Gray II

> **···**
>
> On Sep 19, 2006, at 11:33 PM, Eero Saynatkari wrote:
> 
> > Nguyen Huu Bach wrote:
> > 
> > > This is a simple way to achieve that. Maybe there are others
> > > 
> > > def read\_char
> > 
> > &nbsp;&nbsp;&nbsp;# Save previous state  
> > &nbsp;&nbsp;&nbsp;old = `stty -g`
> > 
> > > system "stty raw -echo"  
> > > STDIN.getc  
> > > ensure
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;system "stty #{old}"
> > 
> > > end
> > > 
> > > good luck
> > 
> > Might be better to use Termios or Curses, though.
