# Keyboard input: one character

**URL:** <https://rubytalk.org/t/keyboard-input-one-character/40314>\
**Category:** ruby-talk\
**Created:** [1 September 2007 07:20 UTC](https://rubytalk.org/t/keyboard-input-one-character/40314 "2007-09-01T07:20:07Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![gu1](https://avatars.discourse-cdn.com/v4/letter/g/9fc29f/32.png) [@gu1](https://rubytalk.org/u/gu1)\
**Post date:** [1 September 2007 07:20 UTC](https://rubytalk.org/t/keyboard-input-one-character/40314/1 "2007-09-01T07:20:07Z")

</div>

hi to all!  
i've seen this question posted many times without a single definitive answer. what i'm trying to get is a single character from the keyboard in a multiplatform solution: i hit the key "h" and rubu knows "h" has been pressed. i need it to detect the imput for a game i'm working on: insertin the key-detection into a loop would actually make the player interact with the game.

i've come across the library highline ( [http://highline.rubyforge.org/](http://highline.rubyforge.org/) ). the simplest program

require "highline/system\_extensions"  
include HighLine::SystemExtensions  
#print "Enter one character: "  
while true  
&nbsp;&nbsp;&nbsp;char = get\_character  
&nbsp;&nbsp;&nbsp;if char != nil  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts char.chr  
&nbsp;&nbsp;&nbsp;end  
end

only works to a certain extend. it puts out the received character only after the end of the program, not during its execution, for some strange reason. moreover it blocks the execution of the program while integrated into a gosu ( [http://code.google.com/p/gosu/](http://code.google.com/p/gosu/) ) framework.

on the other hand, any attempt on using curses results, don't know why, in the error:

LINES value must be \>= 2 and \<= -2616: got 1  
initscr(): LINES=1 COLS=1: too small.

anyone can help?

---

<div class="post-metadata">

**Author:** ![Jon\_Hawkins](https://avatars.discourse-cdn.com/v4/letter/j/958977/32.png) [@Jon\_Hawkins](https://rubytalk.org/u/Jon_Hawkins)\
**Post date:** [1 September 2007 08:40 UTC](https://rubytalk.org/t/keyboard-input-one-character/40314/2 "2007-09-01T08:40:16Z")

</div>

gu wrote:

> i've come across the library highline ( [http://highline.rubyforge.org/](http://highline.rubyforge.org/)  
> ). the simplest program
> 
> require "highline/system\_extensions"  
> include HighLine::SystemExtensions  
> #print "Enter one character: "  
> while true  
> &nbsp;&nbsp;&nbsp;char = get\_character  
> &nbsp;&nbsp;&nbsp;if char != nil  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts char.chr  
> &nbsp;&nbsp;&nbsp;end  
> end

info = gets.chomp  
&nbsp;&nbsp;if info != nil  
puts info  
end

or more elaborately you would use it like the following:

info = gets.chomp  
if info != nil  
&nbsp;&nbsp;puts "You Entered the Character: #{info}"  
end

Hoped this helped.

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

---

<div class="post-metadata">

**Author:** ![gu1](https://avatars.discourse-cdn.com/v4/letter/g/9fc29f/32.png) [@gu1](https://rubytalk.org/u/gu1)\
**Post date:** [1 September 2007 08:50 UTC](https://rubytalk.org/t/keyboard-input-one-character/40314/3 "2007-09-01T08:50:09Z")

</div>

Michael Linfield ha scritto:

> info = gets.chomp  
> &nbsp;&nbsp;if info != nil  
> puts info  
> end

hi and thanks for the reply! well.. the following code

while true  
&nbsp;&nbsp;&nbsp;info = gets.chomp  
&nbsp;&nbsp;&nbsp;if info != nil  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts info  
&nbsp;&nbsp;&nbsp;end  
end

actually doesn't outputs a thing..

---

<div class="post-metadata">

**Author:** ![Jon\_Hawkins](https://avatars.discourse-cdn.com/v4/letter/j/958977/32.png) [@Jon\_Hawkins](https://rubytalk.org/u/Jon_Hawkins)\
**Post date:** [1 September 2007 08:58 UTC](https://rubytalk.org/t/keyboard-input-one-character/40314/4 "2007-09-01T08:58:20Z")

</div>

> while true  
> &nbsp;&nbsp;&nbsp;info = gets.chomp  
> &nbsp;&nbsp;&nbsp;if info != nil  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts info  
> &nbsp;&nbsp;&nbsp;end  
> end
> 
> actually doesn't outputs a thing..

lol. i didnt mean for it to be used in a while loop.

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

---

<div class="post-metadata">

**Author:** ![Jon\_Hawkins](https://avatars.discourse-cdn.com/v4/letter/j/958977/32.png) [@Jon\_Hawkins](https://rubytalk.org/u/Jon_Hawkins)\
**Post date:** [1 September 2007 09:01 UTC](https://rubytalk.org/t/keyboard-input-one-character/40314/5 "2007-09-01T09:01:49Z")

</div>

Michael Linfield wrote:

> > while true  
> > &nbsp;&nbsp;&nbsp;info = gets.chomp  
> > &nbsp;&nbsp;&nbsp;if info != nil  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts info  
> > &nbsp;&nbsp;&nbsp;end  
> > end
> > 
> > actually doesn't outputs a thing..
> 
> lol. i didnt mean for it to be used in a while loop.

And i actually just got home and tested that while loop, it works...ur  
not doing this in IRB right?

testprog.rb

> > while true  
> > &nbsp;&nbsp;&nbsp;info = gets.chomp  
> > &nbsp;&nbsp;&nbsp;if info != nil  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts info  
> > &nbsp;&nbsp;&nbsp;end  
> > end

ruby testprog.rb

output:  
h  
h  
i  
i  
p  
p  
#whatever letter you enter it will respond with that letter that you  
entered.

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

---

<div class="post-metadata">

**Author:** ![gu1](https://avatars.discourse-cdn.com/v4/letter/g/9fc29f/32.png) [@gu1](https://rubytalk.org/u/gu1)\
**Post date:** [1 September 2007 09:20 UTC](https://rubytalk.org/t/keyboard-input-one-character/40314/6 "2007-09-01T09:20:06Z")

</div>

Michael Linfield ha scritto:

> Michael Linfield wrote:
> 
> > > while true  
> > > &nbsp;&nbsp;&nbsp;info = gets.chomp  
> > > &nbsp;&nbsp;&nbsp;if info != nil  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts info  
> > > &nbsp;&nbsp;&nbsp;end  
> > > end
> > > 
> > > actually doesn't outputs a thing..
> > 
> > lol. i didnt mean for it to be used in a while loop.
> 
> And i actually just got home and tested that while loop, it works...ur not doing this in IRB right?
> 
> testprog.rb
> 
> > > while true  
> > > &nbsp;&nbsp;&nbsp;info = gets.chomp  
> > > &nbsp;&nbsp;&nbsp;if info != nil  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts info  
> > > &nbsp;&nbsp;&nbsp;end  
> > > end
> 
> ruby testprog.rb
> 
> output:  
> h  
> i  
> p  
> #whatever letter you enter it will respond with that letter that you entered.

no, i'm not using irb. maybe there's an os compatibility problem going on? i'm testin it in windows xp sp2..

---

<div class="post-metadata">

**Author:** ![Eckhardt\_F](https://avatars.discourse-cdn.com/v4/letter/e/bc79bd/32.png) [@Eckhardt\_F](https://rubytalk.org/u/Eckhardt_F)\
**Post date:** [23 September 2007 19:25 UTC](https://rubytalk.org/t/keyboard-input-one-character/40314/7 "2007-09-23T19:25:08Z")

</div>

Hi,

I think gu means to get one character without pressing enter  
afterwards. Just like "readkey" in Pascal. As I know this is actually  
only possible with Ruby on MS Windows with

(Win32API.new("crtdll", "\_getch", , "L").Call).chr

Bye

> **···**
>
> On Sep 1, 11:01 am, Michael Linfield \<globyy3...@hotmail.com\> wrote:
> 
> > Michael Linfield wrote:
> > 
> > \>\> while true  
> > \>\> info = gets.chomp  
> > \>\> if info != nil  
> > \>\> puts info  
> > \>\> end  
> > \>\> end
> > 
> > \>\> actually doesn't outputs a thing..
> > 
> > \> lol. i didnt mean for it to be used in a while loop.
> > 
> > And i actually just got home and tested that while loop, it works...ur  
> > not doing this in IRB right?
> > 
> > testprog.rb
> > 
> > \>\> while true  
> > \>\> info = gets.chomp  
> > \>\> if info != nil  
> > \>\> puts info  
> > \>\> end  
> > \>\> end
> > 
> > ruby testprog.rb
> > 
> > output:  
> > h  
> > h  
> > i  
> > i  
> > p  
> > p  
> > #whatever letter you enter it will respond with that letter that you  
> > entered.  
> > --  
> > Posted viahttp://www.ruby-forum.com/.

---

<div class="post-metadata">

**Author:** ![Ari\_Brown](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@Ari\_Brown](https://rubytalk.org/u/Ari_Brown)\
**Post date:** [1 September 2007 16:31 UTC](https://rubytalk.org/t/keyboard-input-one-character/40314/8 "2007-09-01T16:31:28Z")

</div>

Well, to help you answer your original question.... Are you using an IDE? A problem that often occurs with Windows IDEs is that it will ask all of the gets() and then puts() them out. In other words, you may have a syncing issue going on.

STDOUT.sync = true

But, in the words of Avdi Grimm (sic), don't make a habit out of it. Synchronous output is a great way to slow down your code.

aRi  
-------------------------------------------|  
Nietzsche is my copilot

> **···**
>
> On Sep 1, 2007, at 5:20 AM, gu wrote:
> 
> > no, i'm not using irb. maybe there's an os compatibility problem going on? i'm testin it in windows xp sp2..

---

<div class="post-metadata">

**Author:** ![Jon\_Hawkins](https://avatars.discourse-cdn.com/v4/letter/j/958977/32.png) [@Jon\_Hawkins](https://rubytalk.org/u/Jon_Hawkins)\
**Post date:** [1 September 2007 19:39 UTC](https://rubytalk.org/t/keyboard-input-one-character/40314/9 "2007-09-01T19:39:00Z")

</div>

Ari Brown wrote:

> **···**
>
> > On Sep 1, 2007, at 5:20 AM, gu wrote:
> > 
> > > no, i'm not using irb. maybe there's an os compatibility problem  
> > > going on? i'm testin it in windows xp sp2..
> 
> > But, in the words of Avdi Grimm (sic), don't make a habit out of it.  
> > Synchronous output is a great way to slow down your code.
> > 
> > aRi
> 
> Some advise: Download Ubuntu...or Debian..whatever suits you...install  
> it on an extra machine...solve about 99% of ur problems. 😉  
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
