Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it possible to use getch() to obtain inputs of varying length?
    text
    copied!<p>I've taken up the adventure of creating a relatively small command-line RPG to flex my newfound Python muscles, but I've already run into a conundrum. I'm using this implementation of getch():</p> <pre><code>def getch(): fd = sys.stdin.fileno() old_settings = termios.tcgetattr(fd) tty.setraw(sys.stdin.fileno()) key = sys.stdin.read(3) termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) </code></pre> <p>I have <code>key</code> set to read in 3 characters to capture arrow keys. Up, for example, is read in as <code>ESC[A</code>; with this method, I can use <code>key[2]</code> to determine if an arrow key was pressed and which one. All well and good, except that I'd also like to capture all sorts of other keys; <code>q</code> for quest log, <code>wasd</code> for movement (pressing the arrows in various orders will be the method of attack), and many others. The problem is instantly clear; if <code>getch()</code> only returns a single character, arrow functionality is lost entirely.</p> <p>I'm contemplating rethinking the arrow system altogether if there is no simple solution, but I'm almost certain there must be. Admittedly, I know little of what is going on within <code>tty</code>, but I read somewhere that if you only read in 1 character, the excess characters from an arrow press are retained in the buffer. How might I go about accessing said buffer? Alternatively, is there some clever way to tell <code>stdin</code> to expect input of variable length?</p> <p>Thank you in advance for any and all assistance.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload