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

David Fuess fuess at att.net
Wed Feb 28 08:33:16 EST 2001


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
<ben.hutchings at roundpoint.com> wrote:

>Jeremy Reed <jpreed00 at 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!




More information about the Python-list mailing list