<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Non blocking console input in Python and Java</title>
	<atom:link href="http://www.darkcoding.net/software/non-blocking-console-io-is-not-possible/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.darkcoding.net/software/non-blocking-console-io-is-not-possible/</link>
	<description>Solvitas perambulum</description>
	<lastBuildDate>Mon, 06 Feb 2012 00:12:49 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: douniwan5788</title>
		<link>http://www.darkcoding.net/software/non-blocking-console-io-is-not-possible/#comment-875</link>
		<dc:creator>douniwan5788</dc:creator>
		<pubDate>Fri, 25 Nov 2011 14:50:55 +0000</pubDate>
		<guid isPermaLink="false">http://www.darkcoding.net/software/non-blocking-console-io-is-not-possible/#comment-875</guid>
		<description>&lt;p&gt;I got an idea , use  PushbackInputStream  encapsulate system.in，always left a char in buffer ，then its become non block,i don&#039;t know wether it works,i am running a test&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>I got an idea , use  PushbackInputStream  encapsulate system.in，always left a char in buffer ，then its become non block,i don&#8217;t know wether it works,i am running a test</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Robbie</title>
		<link>http://www.darkcoding.net/software/non-blocking-console-io-is-not-possible/#comment-874</link>
		<dc:creator>Robbie</dc:creator>
		<pubDate>Fri, 18 Nov 2011 12:38:13 +0000</pubDate>
		<guid isPermaLink="false">http://www.darkcoding.net/software/non-blocking-console-io-is-not-possible/#comment-874</guid>
		<description>&lt;p&gt;Bill, your suggestion is great but only if you don&#039;t want to follow every key press right away.
You don&#039;t see the content of &quot;raw_input()&quot; until &quot;\n&quot; is pressed...&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Bill, your suggestion is great but only if you don&#8217;t want to follow every key press right away.
You don&#8217;t see the content of &#8220;raw_input()&#8221; until &#8220;\n&#8221; is pressed&#8230;</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Simple key listener &#8211; java - Programmers Goodies</title>
		<link>http://www.darkcoding.net/software/non-blocking-console-io-is-not-possible/#comment-873</link>
		<dc:creator>Simple key listener &#8211; java - Programmers Goodies</dc:creator>
		<pubDate>Mon, 14 Nov 2011 18:36:26 +0000</pubDate>
		<guid isPermaLink="false">http://www.darkcoding.net/software/non-blocking-console-io-is-not-possible/#comment-873</guid>
		<description>&lt;p&gt;[...] This page presents a method of setting the console into non-blocking mode in order to read a character, which you could use to break your loop. It also presents a few other methods for both Python and Java, but it has to be considered somewhat hacky and non-portable (wouldn&#8217;t work under Windows for example). I don&#8217;t think there is a &#8216;nice&#8217; easy way to do it I&#8217;m afraid. [...]&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>[...] This page presents a method of setting the console into non-blocking mode in order to read a character, which you could use to break your loop. It also presents a few other methods for both Python and Java, but it has to be considered somewhat hacky and non-portable (wouldn&#8217;t work under Windows for example). I don&#8217;t think there is a &#8216;nice&#8217; easy way to do it I&#8217;m afraid. [...]</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Graham King</title>
		<link>http://www.darkcoding.net/software/non-blocking-console-io-is-not-possible/#comment-872</link>
		<dc:creator>Graham King</dc:creator>
		<pubDate>Fri, 30 Sep 2011 16:58:59 +0000</pubDate>
		<guid isPermaLink="false">http://www.darkcoding.net/software/non-blocking-console-io-is-not-possible/#comment-872</guid>
		<description>&lt;p&gt;@ro0t  You still need to set your console to raw mode, or readline (in your console) won&#039;t give you any data until Enter is pressed.&lt;/p&gt;

&lt;p&gt;In my example I&#039;m printing numbers, so I need a busy loop. In the general case yes poll is much nicer, thanks. If we&#039;re only using poll for this case, I don&#039;t think you even need to check the return value, so you have:&lt;/p&gt;

&lt;pre&gt;
import sys, select, tty, termios
old_settings = termios.tcgetattr(sys.stdin)
try:
    tty.setcbreak(sys.stdin.fileno())
    poll = select.poll()
    poll.register(sys.stdin, select.POLLIN)
    while 1:
        poll.poll()
        c = sys.stdin.read(1)
        print(c)
finally:
    termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_settings)
&lt;/pre&gt;
</description>
		<content:encoded><![CDATA[<p>@ro0t  You still need to set your console to raw mode, or readline (in your console) won&#8217;t give you any data until Enter is pressed.</p>

<p>In my example I&#8217;m printing numbers, so I need a busy loop. In the general case yes poll is much nicer, thanks. If we&#8217;re only using poll for this case, I don&#8217;t think you even need to check the return value, so you have:</p>

<pre>
import sys, select, tty, termios
old_settings = termios.tcgetattr(sys.stdin)
try:
    tty.setcbreak(sys.stdin.fileno())
    poll = select.poll()
    poll.register(sys.stdin, select.POLLIN)
    while 1:
        poll.poll()
        c = sys.stdin.read(1)
        print(c)
finally:
    termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_settings)
</pre>]]></content:encoded>
	</item>
	<item>
		<title>By: ro0t</title>
		<link>http://www.darkcoding.net/software/non-blocking-console-io-is-not-possible/#comment-871</link>
		<dc:creator>ro0t</dc:creator>
		<pubDate>Mon, 13 Jun 2011 21:30:31 +0000</pubDate>
		<guid isPermaLink="false">http://www.darkcoding.net/software/non-blocking-console-io-is-not-possible/#comment-871</guid>
		<description>&lt;p&gt;learn about poll system call...
on Python, lets use poll since is portable:&lt;/p&gt;

&lt;pre&gt;
poll = select.poll()
poll.register(sys.stdin, select.POLLIN)
events = poll.poll()
for fid, event in events:
    if fid == sys.stdin.fileno():
        in = raw_input()
        print &#039;user,says something:&#039;, in
    else:
       make something else...
&lt;/pre&gt;
</description>
		<content:encoded><![CDATA[<p>learn about poll system call&#8230;
on Python, lets use poll since is portable:</p>

<pre>
poll = select.poll()
poll.register(sys.stdin, select.POLLIN)
events = poll.poll()
for fid, event in events:
    if fid == sys.stdin.fileno():
        in = raw_input()
        print 'user,says something:', in
    else:
       make something else...
</pre>]]></content:encoded>
	</item>
	<item>
		<title>By: Nemesis Fixx</title>
		<link>http://www.darkcoding.net/software/non-blocking-console-io-is-not-possible/#comment-870</link>
		<dc:creator>Nemesis Fixx</dc:creator>
		<pubDate>Thu, 09 Jun 2011 11:32:34 +0000</pubDate>
		<guid isPermaLink="false">http://www.darkcoding.net/software/non-blocking-console-io-is-not-possible/#comment-870</guid>
		<description>&lt;p&gt;Thanks Graham;
 your python non-blocking solution for the console  is elegant (python is prose with power).&lt;/p&gt;

&lt;p&gt;Though I particularly failed to get Bill Hamilton&#039;s said &#039;elegant&#039; solution to pull off the trick, this is how I combined both your approaches to do the thing with threading thrown in:&lt;/p&gt;

&lt;pre&gt;
import sys
import select
import tty
import termios
from threading import Thread

program_run = True
input_thread_timeout = 0.005 #seconds
quit_key = &#039;\x1b&#039; # x1b is ESC

#check stdin for input...
def isData():
        return select.select([sys.stdin], [], [], 0) == ([sys.stdin], [], [])

#check n terminate program on terminal condition,
#from a separate thread
class waitOnInput(Thread):
    def run(self):
        old_settings = termios.tcgetattr(sys.stdin)
        try:
            tty.setcbreak(sys.stdin.fileno())
            global program_run
            thread_run = True
            while thread_run:
                if isData():
                    c = sys.stdin.read(1)
                    if c == quit_key:
                        break
                        thread_run = False
                        program_run = False
        finally:
            termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_settings)
            thread_run = False


t = waitOnInput()

#start work here...
i = 1

while program_run :
    if not t.is_alive():
        t.start()

    #test for terminal condition or timeout...
    t.join(input_thread_timeout)

    if t.is_alive():
        #continue work here...
        print i
        i += 1
    else:
        break
&lt;/pre&gt;
</description>
		<content:encoded><![CDATA[<p>Thanks Graham;
 your python non-blocking solution for the console  is elegant (python is prose with power).</p>

<p>Though I particularly failed to get Bill Hamilton&#8217;s said &#8216;elegant&#8217; solution to pull off the trick, this is how I combined both your approaches to do the thing with threading thrown in:</p>

<pre>
import sys
import select
import tty
import termios
from threading import Thread

program_run = True
input_thread_timeout = 0.005 #seconds
quit_key = '\x1b' # x1b is ESC

#check stdin for input...
def isData():
        return select.select([sys.stdin], [], [], 0) == ([sys.stdin], [], [])

#check n terminate program on terminal condition,
#from a separate thread
class waitOnInput(Thread):
    def run(self):
        old_settings = termios.tcgetattr(sys.stdin)
        try:
            tty.setcbreak(sys.stdin.fileno())
            global program_run
            thread_run = True
            while thread_run:
                if isData():
                    c = sys.stdin.read(1)
                    if c == quit_key:
                        break
                        thread_run = False
                        program_run = False
        finally:
            termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_settings)
            thread_run = False


t = waitOnInput()

#start work here...
i = 1

while program_run :
    if not t.is_alive():
        t.start()

    #test for terminal condition or timeout...
    t.join(input_thread_timeout)

    if t.is_alive():
        #continue work here...
        print i
        i += 1
    else:
        break
</pre>]]></content:encoded>
	</item>
	<item>
		<title>By: Strawbot</title>
		<link>http://www.darkcoding.net/software/non-blocking-console-io-is-not-possible/#comment-869</link>
		<dc:creator>Strawbot</dc:creator>
		<pubDate>Fri, 13 May 2011 18:31:29 +0000</pubDate>
		<guid isPermaLink="false">http://www.darkcoding.net/software/non-blocking-console-io-is-not-possible/#comment-869</guid>
		<description>&lt;p&gt;the spam protection math below should accept a text answer since it&#039;s query is in text.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>the spam protection math below should accept a text answer since it&#8217;s query is in text.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Strawbot</title>
		<link>http://www.darkcoding.net/software/non-blocking-console-io-is-not-possible/#comment-868</link>
		<dc:creator>Strawbot</dc:creator>
		<pubDate>Fri, 13 May 2011 18:30:13 +0000</pubDate>
		<guid isPermaLink="false">http://www.darkcoding.net/software/non-blocking-console-io-is-not-possible/#comment-868</guid>
		<description>&lt;p&gt;Thanks to all those above. I thought I&#039;d post my interpretation of the above information for a simple test program that works on macs and windows. This reads and prints 10 characters with no echoes of character and it blocks on the single character:&lt;/p&gt;

&lt;pre&gt;
import sys
try:
    if sys.platform == &#039;win32&#039;:
        import msvcrt
        def getKey():  // blocking read of one character for windows
            return msvcrt.getch()
    else:
        import termios, tty
        old_settings = termios.tcgetattr(sys.stdin)
        tty.setcbreak(sys.stdin.fileno())

        def getKey(): // blocking read of one character for mac
            return sys.stdin.read(1)

    for i in range(10):
        k = getKey()
        print k
finally:
    if sys.platform != &#039;win32&#039;:
        import termios
        termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_settings)
&lt;/pre&gt;
</description>
		<content:encoded><![CDATA[<p>Thanks to all those above. I thought I&#8217;d post my interpretation of the above information for a simple test program that works on macs and windows. This reads and prints 10 characters with no echoes of character and it blocks on the single character:</p>

<pre>
import sys
try:
    if sys.platform == 'win32':
        import msvcrt
        def getKey():  // blocking read of one character for windows
            return msvcrt.getch()
    else:
        import termios, tty
        old_settings = termios.tcgetattr(sys.stdin)
        tty.setcbreak(sys.stdin.fileno())

        def getKey(): // blocking read of one character for mac
            return sys.stdin.read(1)

    for i in range(10):
        k = getKey()
        print k
finally:
    if sys.platform != 'win32':
        import termios
        termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_settings)
</pre>]]></content:encoded>
	</item>
	<item>
		<title>By: Aubrey Bourke</title>
		<link>http://www.darkcoding.net/software/non-blocking-console-io-is-not-possible/#comment-867</link>
		<dc:creator>Aubrey Bourke</dc:creator>
		<pubDate>Wed, 25 Aug 2010 18:55:28 +0000</pubDate>
		<guid isPermaLink="false">http://www.darkcoding.net/software/non-blocking-console-io-is-not-possible/#comment-867</guid>
		<description>&lt;p&gt;Hi Graham,&lt;/p&gt;

&lt;p&gt;Thanks for taking the time to provide the non blocking example in Java. Your code works well.&lt;/p&gt;

&lt;p&gt;However, when I add to your code, with other character input, like this:&lt;/p&gt;

&lt;pre&gt;
 if ( System.in.available() != 0 ) {
    int c = System.in.read();
    if ( c == 0x1B ) {
        break;
    }
    if (c ==0x1F ){
        System.out.println(&quot;down arrow pressed&quot;)&#039;
    }

}
&lt;/pre&gt;

&lt;p&gt;it unexpectedly breaks every time, even when i hit the down arrow key!!&lt;/p&gt;

&lt;p&gt;Why does that happen? &amp; What should I do to get it to work?&lt;/p&gt;

&lt;p&gt;Regards
Aubrey.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Hi Graham,</p>

<p>Thanks for taking the time to provide the non blocking example in Java. Your code works well.</p>

<p>However, when I add to your code, with other character input, like this:</p>

<pre>
 if ( System.in.available() != 0 ) {
    int c = System.in.read();
    if ( c == 0x1B ) {
        break;
    }
    if (c ==0x1F ){
        System.out.println("down arrow pressed")'
    }

}
</pre>

<p>it unexpectedly breaks every time, even when i hit the down arrow key!!</p>

<p>Why does that happen? &amp; What should I do to get it to work?</p>

<p>Regards
Aubrey.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Bill Hamilton</title>
		<link>http://www.darkcoding.net/software/non-blocking-console-io-is-not-possible/#comment-866</link>
		<dc:creator>Bill Hamilton</dc:creator>
		<pubDate>Mon, 21 Dec 2009 20:14:18 +0000</pubDate>
		<guid isPermaLink="false">http://www.darkcoding.net/software/non-blocking-console-io-is-not-possible/#comment-866</guid>
		<description>&lt;p&gt;Hey There,&lt;/p&gt;

&lt;p&gt;I find your solution was not very elegant in python. Unfortunately Im using mac and although I LOVE pygame, I cant seem to get it to work. Also Im using console and don&#039;t want to be unix specific. So what I did is this:&lt;/p&gt;

&lt;pre&gt;
from threading import Thread
program_run = True

class waitOnInput(Thread):
    def run(self):
    global program_run
    thread_run = True
    while thread_run :
            data = raw_input(&#039;input command (q for quit)-&gt;&#039;)
        print &#039;got input : &#039;, data
        if data == &#039;q&#039; :
            thread_run = False
        program_run = False

while program_run :
    t = waitOnInput()
    t.start()
    #do stuff

t.join()
&lt;/pre&gt;
</description>
		<content:encoded><![CDATA[<p>Hey There,</p>

<p>I find your solution was not very elegant in python. Unfortunately Im using mac and although I LOVE pygame, I cant seem to get it to work. Also Im using console and don&#8217;t want to be unix specific. So what I did is this:</p>

<pre>
from threading import Thread
program_run = True

class waitOnInput(Thread):
    def run(self):
    global program_run
    thread_run = True
    while thread_run :
            data = raw_input('input command (q for quit)-&gt;')
        print 'got input : ', data
        if data == 'q' :
            thread_run = False
        program_run = False

while program_run :
    t = waitOnInput()
    t.start()
    #do stuff

t.join()
</pre>]]></content:encoded>
	</item>
</channel>
</rss>

