Check for keypress on Linux xterm ?

Grant Edwards grante at visi.com
Tue Apr 10 12:15:44 EDT 2007


On 2007-04-10, hlubenow <hlubenow2 at gmx.net> wrote:

>>> My problem is, I don't want my program to wait for the
>>> keypress. I just want to check, if a key is currently pressed
>>> and if not, I'd like to continue with my program (like
>>> "INKEY$" in some BASIC-dialects).
>> 
>> The answer to this frequently asked question is actually in the FAQ:
>
> http://www.python.org/doc/faq/library.html#how-do-i-get-a-single-keypress-at-a-time
>
> You're answer is only less than half correct:
>
> Most of the given examples use something like
>
> c = sys.stdin.read(1)
>
> like my example does. This blocks input.

read() will not block if the file has been set to non-blocking
mode.  That's what these two lines in the FAQ answer do:

  oldflags = fcntl.fcntl(fd, fcntl.F_GETFL)
  fcntl.fcntl(fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK)

I do make mistakes, but before telling somebody he's wrong, it
might be a good idea to actually try what he's suggested. ;)

-- 
Grant Edwards                   grante             Yow!  Edwin Meese made me
                                  at               wear CORDOVANS!!
                               visi.com            



More information about the Python-list mailing list