What is the correct way to read sys.stdin in iPyth

2019-07-04 12:09发布

问题:

I'm trying to write a library that will work as well across platforms as standard Python, iPython and within qtConsole.

Everything works except my keyboard interaction.

I want things like 'press any key' or 'press Y/N/Esc'

In Windows this is easy with msvcrt and kbhit. (though I've not tried qtconsole in windows) In Linux this works using stdin and termios under both Python and iPython.

The trouble is it seems qtconsole uses non-blocking stdin and does not support termios.

You would think this would be a good thing, simply flush stdin then check it for a character. The problem is this seems not to work. It never returns a character. It is as though ipython itself is getting in the way of stdin being fed by the terminal.

What is the correct way to read sys.stdin in qtconsole?

(BTW, there is no tag for qtconsole, this question is about qtconsole, not just iPython)