title: Python (preferably 3) equivalent to INKEY$
body: <p>I am using Python 3.6.4 on a Windows 7 system (I have other systems like Win 10 and Android but this is my starting point).</p>
  
  <p>INKEY$, for those not familiar with BASIC (pretty much any flavor), is a function that checks the keyboard buffer, returning that data as a string or a null/empty value ("") if there was no data, and clears the buffer. The length of the returned string depends on the data in the buffer, normally 0, 1, or 2 on a single keystroke (a fast typist could fill the small buffer between checks in the old days). The Enter key was not needed (unless that was what you were looking for) or processed and the program did not pause unless programmed to do so.</p>
  
  <p>Pauser:</p>
  
  <pre><code>    a$=""
      while a$=""
      a$=inkey$
      wend
  </code></pre>
  
  <p>Flow Interrupter:</p>
  
  <pre><code>    a=0
      while a &lt; 1000
          a=a+1
          print a
          a$=inkey$
          if a$&lt;&gt;"" then exit while
      wend
  </code></pre>
  
  <p>Quick parser:</p>
  
  <pre><code>    a$=inkey$
      if a$&lt;&gt;"" then
          rem process input
          rem like arrow keys/a w s z for directional movement
          rem useful for games and custom editors
      end if
  </code></pre>
  
  <p>I am wanting to know if Python has a simple cross platform function (ie not 10+ lines of code unless in an importable module/class) that is the equivalent to the INKEY$ function?  Also, I am not wanting to import the gaming module(s), just want an INKEY$ function equivalent (simple, straight forth, small).</p>
  
  <pre class="lang-py prettyprint-override"><code>    import inkey
      a = inkey.inkey()
  </code></pre>
  
  <p>Update #1:
  After I installed the readchar module and corrected a reported error by Python (stdout.write(a) needs to be stdout.write(str(a)) as the variable 'a' appears to be returned as a byte string from readchar() function) when using the code listed by Mr. Stratton below, I only get continuous stream of b'\xff' and console echoed characters if there where any keypresses.</p>
  
  <p>Stripping it down to use only the function doesn't help either:</p>
  
  <pre class="lang-py prettyprint-override"><code>    from readchar import readchar
      from sys import stdout
      import os
  
      #I like clear screens, and I can not lie
      os.system('cls')  # For Windows
      #os.system('clear')  # For Linux/OS X
  
      def inkey():
          "INKEY$ function"
          return readchar()
          #let the processing hit the floor, elsewhere
  
      b=0
      step = 1
      while b &lt; 1000:
          b = b + step
          print(b)
          #convert bytes to integers
          a = int.from_bytes(inkey(), "big")
          #remember I keep getting b'\xff' (255) when buffer is empty
          if chr(a) == "a":
              step = step - 1
              a = 255 #don't stop
          if chr(a) == "l":
              step = step + 1
              a = 255 #don't stop
          if a != 255:
              break 
  </code></pre>
  
  <p>It is supposed to count b from 0 to 999, stopping on almost any keypress, 'a' decreases the step, 'l' increases it.  Instead, it prints the keypress either before or after the value of b depending on timing and continues until b = 1000. Nothing I did made a difference.</p>
  
  <p>While the Pauser function can be replaced with an input() (i = input("Press Enter key to continue")) the other two variants can't be changed so easily it seems.</p>