0

I am using Python2 on Ubuntu, and I would like to write a loop which waits for either (i) a particular key to be pressed, or (ii) another function to return True.

For example:

while not (check_key() == 'n' or foo() == True):
    continue

The problem is, all solutions I can find for the check_key() function are blocking: they wait until a key is pressed before returning. This means that my foo() function will never be evaluated.

Are there any solutions for check_key() which will check whether a key is currently being pressed, and if so, return the value of that key, but otherwise will return nothing and allow foo() to be evaluated?

Karnivaurus
  • 22,823
  • 57
  • 147
  • 247
  • This is answered here: https://stackoverflow.com/questions/2408560/python-nonblocking-console-input – mikea Sep 05 '19 at 13:01
  • 1
    Possible duplicate of [Python nonblocking console input](https://stackoverflow.com/questions/2408560/python-nonblocking-console-input) – mikea Sep 05 '19 at 13:01

0 Answers0