# Non-blocking 'gets'?

**URL:** <https://www.ruby-forum.com/t/non-blocking-gets/116247>\
**Category:** Ruby\
**Created:** [October 3, 2007, 7:46pm UTC](https://www.ruby-forum.com/t/non-blocking-gets/116247 "2007-10-03T19:46:24Z")\
**Posts on this page:** 8\
**Page:** 2

<div class="post-metadata">

**Author:** ![sgware](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@sgware](https://www.ruby-forum.com/u/sgware)\
**Post date:** [October 4, 2007, 8:34am UTC](https://www.ruby-forum.com/t/non-blocking-gets/116247/21 "2007-10-04T08:34:54Z")

</div>

On 10/3/07, Stephen W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Is there a way to perform a non-blocking gets or getc? As in a method  
> that reads a character from stdin if there is one in the buffer or  
> returns nil otherwise?

If I was programming a Windows console app in C, I would use kbhit()  
to check if there was a character in the buffer. So I decided this  
was a good time to try writing my first ruby c extension.

on Windows, save and complie conio.c into conio.dll, and then the  
following example should work.  
-Adam

— test\_conio.rb —  
require ‘conio’  
#background work:  
Thread.new { print(‘.’) or sleep(1) while true }  
#check for input in this thread  
Thread.new do  
while 1  
while !CONIO::kbhit?; end #–wait for key, nonblocking  
#s = STDIN::getc #–this blocks until return pressed  
s = CONIO::getch #–this doesn’t  
p “#{s} (#{s.chr})”  
exit if s==?q  
end  
end  
#do some foreground work  
puts(“waiting”) or sleep(10) while 1

* * *

—conio.c—  
/\* conio.c – Windows conio functions  
3 Oct 2007, Adam S.  
Ruby license applies  
\*/  
#include “ruby.h”  
#include \<conio.h\>

static VALUE c\_kbhit(VALUE self)  
{  
return \_kbhit()? Qtrue : Qfalse;  
}

static VALUE c\_getch(VALUE self)  
{  
int n = \_getch();  
return INT2FIX(n);  
}  
static VALUE c\_ungetch(VALUE self, VALUE cv)  
{  
int c = FIX2INT(cv);  
int n = \_ungetch(c);  
return INT2FIX(n);  
}

VALUE mConio;

\_\_declspec( dllexport ) void Init\_conio()  
{  
mConio = rb\_define\_module(“CONIO”);  
rb\_define\_module\_function(mConio, “kbhit?”, c\_kbhit, 0);  
rb\_define\_module\_function(mConio, “getch”, c\_getch, 0);  
rb\_define\_module\_function(mConio, “ungetch”, c\_ungetch, 1);  
}

---

<div class="post-metadata">

**Author:** ![sgware](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@sgware](https://www.ruby-forum.com/u/sgware)\
**Post date:** [October 4, 2007, 3:45pm UTC](https://www.ruby-forum.com/t/non-blocking-gets/116247/22 "2007-10-04T15:45:32Z")

</div>

On Oct 3, 2007, at 12:46 PM, Stephen W. wrote:

> Is there a way to perform a non-blocking gets or getc?

I vote that you let HighLine do the work for you:

[http://blog.grayproductions.net/articles/i\_just\_want\_one\_character](http://blog.grayproductions.net/articles/i_just_want_one_character)

James Edward G. II

---

<div class="post-metadata">

**Author:** ![sgware](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@sgware](https://www.ruby-forum.com/u/sgware)\
**Post date:** [October 4, 2007, 3:36pm UTC](https://www.ruby-forum.com/t/non-blocking-gets/116247/23 "2007-10-04T15:36:16Z")

</div>

Wow, that is quite complex. Thank you!

I also managed to discover another usable method: run a ‘system’ command  
in a new thread. That seems to release the blocking on gets by creating  
a second process, and as far as I can tell will work on both Windows and  
\*nix. The downside is that the two processes can’t really communicate  
with each other much, but in my case I don’t need them to.

Thanks everyone for you help 🙂

---

<div class="post-metadata">

**Author:** ![sgware](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@sgware](https://www.ruby-forum.com/u/sgware)\
**Post date:** [October 31, 2007, 6:21pm UTC](https://www.ruby-forum.com/t/non-blocking-gets/116247/24 "2007-10-31T18:21:30Z")

</div>

Why don’t you just bind a keypress event? That would capture any given  
keystroke in a non blocking way.

---

<div class="post-metadata">

**Author:** ![sgware](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@sgware](https://www.ruby-forum.com/u/sgware)\
**Post date:** [October 31, 2007, 4:28pm UTC](https://www.ruby-forum.com/t/non-blocking-gets/116247/25 "2007-10-31T16:28:02Z")

</div>

Stephen W. wrote:

> Wow, that is quite complex. Thank you!
> 
> I also managed to discover another usable method: run a ‘system’ command  
> in a new thread. That seems to release the blocking on gets by creating  
> a second process, and as far as I can tell will work on both Windows and  
> \*nix. The downside is that the two processes can’t really communicate  
> with each other much, but in my case I don’t need them to.
> 
> Thanks everyone for you help 🙂

Stephen,

Can you please provide a very simple ‘system’ command example as I’m  
having the same problem.

---

<div class="post-metadata">

**Author:** ![sgware](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@sgware](https://www.ruby-forum.com/u/sgware)\
**Post date:** [October 31, 2007, 6:38pm UTC](https://www.ruby-forum.com/t/non-blocking-gets/116247/26 "2007-10-31T18:38:17Z")

</div>

Lloyd L. wrote:

> Why don’t you just bind a keypress event? That would capture any given  
> keystroke in a non blocking way.

Can you provide an example?

---

<div class="post-metadata">

**Author:** ![sgware](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@sgware](https://www.ruby-forum.com/u/sgware)\
**Post date:** [November 1, 2007, 2:04am UTC](https://www.ruby-forum.com/t/non-blocking-gets/116247/27 "2007-11-01T02:04:12Z")

</div>

Wurzel Cidermaker wrote:

> Lloyd L. wrote:
> 
> > Why don’t you just bind a keypress event? That would capture any given  
> > keystroke in a non blocking way.
> 
> Can you provide an example?

I got the idea from the pickaxe book starting on page 246.

---

<div class="post-metadata">

**Author:** ![sgware](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/48db29/32.png) [@sgware](https://www.ruby-forum.com/u/sgware)\
**Post date:** [March 26, 2014, 12:51pm UTC](https://www.ruby-forum.com/t/non-blocking-gets/116247/28 "2014-03-26T12:51:56Z")

</div>

Stephen W. wrote in post #565434:

> Hi guys, sorry if this is a stupid question, but I’ve been reading  
> around online and in ‘Programming Ruby,’ and I can’t find anything.
> 
> Is there a way to perform a non-blocking gets or getc? As in a method  
> that reads a character from stdin if there is one in the buffer or  
> returns nil otherwise? I’m writing a multi-threaded application that  
> communicates over the internet using sockets, but I want the person who  
> runs the script on the host machine to be able to enter commands via  
> stdin. The ‘gets’ command hangs all my threads until it returns.
> 
> Any suggestions? Thanks!  
> -Stephen

I know this is an old post, but still appears among Google’s top  
results.

By combinig the various solutions I just read, I came up with a  
cross-platform way to solve that problem.  
Details here:

> **[How to read one non-blocking key press in Ruby – Muriel's Tech Blog](https://blog.x-aeon.com/2014/03/26/how-to-read-one-non-blocking-key-press-in-ruby/)**

The relevant piece of code: a `GetKey.getkey` method  
returning the ASCII code or `nil` if none was pressed.

Should work both on Windows and Unix.

module GetKey

# Check if Win32API is accessible or not

@use\_stty = begin  
require ‘Win32API’  
false  
rescue LoadError  
# Use Unix way  
true  
end

# Return the ASCII code last key pressed, or nil if none

# 

# Return::

# \* _Integer_: ASCII code of the last key pressed, or nil if none

def self.getkey  
if @use\_stty  
system(‘stty raw -echo’) # =\> Raw mode, no echo  
char = (STDIN.read\_nonblock(1).ord rescue nil)  
system(‘stty -raw echo’) # =\> Reset terminal mode  
return char  
else  
return Win32API.new(‘crtdll’, ‘\_kbhit’, , ‘I’).Call.zero? ? nil  
: Win32API.new(‘crtdll’, ‘\_getch’, , ‘L’).Call  
end  
end

end

And here is a simple program to test it:

loop do  
k = GetKey.getkey  
puts “Key pressed: #{k.inspect}”  
sleep 1  
end

In the link provided above, I also show how to use the `curses` library,  
but the result gets a bit whacky on Windows.

Hope this will help others.

[Previous page](https://www.ruby-forum.com/t/non-blocking-gets/116247.md?page=1)
