Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Anyone know of a way to get non-blocking keyboard input?

2 views
Skip to first unread message

Ben Hutchings

unread,
Feb 27, 2001, 6:54:55 PM2/27/01
to
Jeremy Reed <jpre...@MailAndNews.com> writes:

> I am looking to create a telnet client that will, by using the select()
> statment, poll an open socket for data to be received while at the same time
> keep accepting keyboard input from the user.
>
> I have tried to run a thread in the background that collects input, but I
> never can figure out how to achieve the 'fluidity' that I desire--i.e. no
> stopping for a return-key press.

I think this ought to work for some platforms:

try:
istty = sys.stdin.isatty()
except AttributeError:
istty = 0

if istty:
if os.name=='posix':
import tty
tty.setraw(sys.stdin.fileno())
elif os.name=='nt':
import win32file, win32con
hstdin = win32file._get_osfhandle(sys.stdin.fileno())
modes = (win32file.GetConsoleMode(hstdin)
& ~(win32con.ENABLE_LINE_INPUT
|win32con.ENABLE_ECHO_INPUT))
win32file.SetConsoleMode(hstdin, modes)

Unfortunately, GetConsoleMode, SetConsoleMode, and associated
constants don't seem to be included in win32all yet!

--
Any opinions expressed are my own and not necessarily those of Roundpoint.

David Fuess

unread,
Feb 28, 2001, 8:33:16 AM2/28/01
to
Assuming you are on a Windows platform ...

--- Begin Demo Script
from msvcrt import *

ch = 0
while ch != 'x':
if kbhit():
ch = getch()
print ch
--- End Demo Script

Dave

On 27 Feb 2001 15:54:55 -0800, Ben Hutchings

Chris Gonnerman

unread,
Feb 28, 2001, 8:57:14 AM2/28/01
to pytho...@python.org
DOH! Boy did I send him the long way around! <slap self>
0 new messages