# IO#getc behavior

**URL:** <https://rubytalk.org/t/io-getc-behavior/21101>\
**Category:** ruby-talk\
**Created:** [26 September 2005 10:59 UTC](https://rubytalk.org/t/io-getc-behavior/21101 "2005-09-26T10:59:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kroeger\_Simon\_ext](https://avatars.discourse-cdn.com/v4/letter/k/5daacb/32.png) [@Kroeger\_Simon\_ext](https://rubytalk.org/u/Kroeger_Simon_ext)\
**Post date:** [26 September 2005 10:59 UTC](https://rubytalk.org/t/io-getc-behavior/21101/1 "2005-09-26T10:59:23Z")

</div>

yes,

but a very platform specific:

> **···**
>
> -------------------------------------------------  
> require 'Win32API'
> 
> $stdout.sync=true  
> kbhit = Win32API.new("msvcrt", "\_kbhit", , 'I')  
> getch = Win32API.new("msvcrt", "\_getch", , 'I')
> 
> while true  
> &nbsp;&nbsp;&nbsp;&nbsp;sleep 0.01 while kbhit.call.zero?  
> &nbsp;&nbsp;&nbsp;&nbsp;c = getch.call  
> &nbsp;&nbsp;&nbsp;&nbsp;puts(c.chr)  
> end  
> -------------------------------------------------
> 
> cheers
> 
> Simon
> 
> > -----Original Message-----  
> > From: Jon A. Lambert [mailto:jlsysinc@alltel.net]  
> > Sent: Monday, September 26, 2005 11:14 AM  
> > To: ruby-talk ML  
> > Subject: IO#getc behavior
> > 
> > I'm writing a console application with an input loop like the  
> > following:
> > 
> > &nbsp;&nbsp;&nbsp;while running  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;keyhit = select( [$stdin], nil, nil, 0.1 )  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if keyhit  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c = $stdin.getc  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session.message(c.chr)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;end
> > 
> > In the cygwin version of Ruby all is fine. The user hits a  
> > key select()  
> > indicates it and the character is gotten with $stdin.getc.
> > 
> > However I ran this under the msvc version of windows and what  
> > happens is  
> > select() returns a ready state for $stdin whether a key is  
> > hit or not.  
> > Then getc() blocks? because there's nothing yet to be gotten.  
> > Worse no I/O  
> > seems to be going on until the user hits the return key and  
> > then whammo I  
> > see either the end result of the loops or it appears more  
> > likely getc blocks  
> > internally under cover until it sees a CR in the stream.
> > 
> > Is there a workaround?
> > 
> > Thanks
> > 
> > --  
> > J Lambert

---

<div class="post-metadata">

**Author:** ![Jon\_A\_Lambert2](https://avatars.discourse-cdn.com/v4/letter/j/43a26b/32.png) [@Jon\_A\_Lambert2](https://rubytalk.org/u/Jon_A_Lambert2)\
**Post date:** [27 September 2005 05:01 UTC](https://rubytalk.org/t/io-getc-behavior/21101/2 "2005-09-27T05:01:25Z")

</div>

Thanks Simon and Sean..

By way of followup to anyone having similar problems ...

I had success with this:

if RUBY\_PLATFORM =~ /win32/  
&nbsp;&nbsp;require 'Win32API'  
&nbsp;&nbsp;Kbhit = Win32API.new("msvcrt", "\_kbhit", [], 'I')  
&nbsp;&nbsp;Getch = Win32API.new("msvcrt", "\_getch", [], 'I')  
&nbsp;&nbsp;def getkey  
&nbsp;&nbsp;&nbsp;&nbsp;sleep 0.01  
&nbsp;&nbsp;&nbsp;&nbsp;return nil if Kbhit.call.zero?  
&nbsp;&nbsp;&nbsp;&nbsp;c = Getch.call  
&nbsp;&nbsp;&nbsp;&nbsp;c = Getch.call + 256 if c.zero? || c == 0xE0  
&nbsp;&nbsp;&nbsp;&nbsp;c  
&nbsp;&nbsp;end  
else  
&nbsp;&nbsp;def getkey  
&nbsp;&nbsp;&nbsp;&nbsp;select( [$stdin], nil, nil, 0.01 ) ? c = $stdin.getc : c = nil  
&nbsp;&nbsp;end  
end

You've got to check getch for extended scancodes.

Also I use an echo/no echo option later in the code and  
found getche() works like Unices getc() in that regard and to get Unices getc() to not echo and still allow CTL-C interrupts  
handled by Ruby I had to do this upon starting...

&nbsp;&nbsp;&nbsp;&nbsp;if !WIN32 && $opts.echo  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;system('stty cbreak -echo')  
&nbsp;&nbsp;&nbsp;&nbsp;end

and this upon exiting..

&nbsp;&nbsp;&nbsp;&nbsp;if !WIN32 && $opts.echo  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;system('stty -cbreak echo')  
&nbsp;&nbsp;&nbsp;&nbsp;end

> **···**
>
> --  
> J Lambert

---

<div class="post-metadata">

**Author:** ![Sean\_O\_Halpin](https://avatars.discourse-cdn.com/v4/letter/s/ac91a4/32.png) [@Sean\_O\_Halpin](https://rubytalk.org/u/Sean_O_Halpin)\
**Post date:** [27 September 2005 09:08 UTC](https://rubytalk.org/t/io-getc-behavior/21101/3 "2005-09-27T09:08:59Z")

</div>

I'm sure I read somewhere that Win32API is being phased out in favour of DL

Sean

> **···**
>
> On 9/26/05, Kroeger Simon (ext) \<simon.kroeger.ext@siemens.com\> wrote:
> 
> > require 'Win32API'
