Yes there are lots of suggested ways, the point is, its not built into the language. Basic, its built in, its one line. Its obscene that we have to google on it.
On Sun, Feb 19, 2017 at 12:13 AM, Douglas Crawford via vcf-midatlantic <vcf-midatlantic@lists.vintagecomputerfederation.org> wrote:
:)
How to poll the keyboard for a keypress, non blocking.
Python
#!/usr/bin/env python import __future__ import sys if sys.version_info.major < 3: import thread as _thread else: import _thread import time try: from msvcrt import getch # try to import Windows version except ImportError: def getch(): # define non-Windows version import tty, termios fd = sys.stdin.fileno() old_settings = termios.tcgetattr(fd) try: tty.setraw(sys.stdin.fileno()) ch = sys.stdin.read(1) finally: termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) return ch char = None def keypress(): global char char = getch() _thread.start_new_thread(keypress, ()) while True: if char is not None: print("Key pressed is " + char.decode('utf-8')) break print("Program is running") time.sleep(5) PureBasic
Returns a character string if a key is pressed during the call of Inkey(). It doesn't interrupt (halt) the program flow.
If special keys (non-ASCII) have to be handled, RawKey() should be called after Inkey().
k$ = Inkey()
I use python for some little things here tooNever having tried this in python, so I had to look this upnot sure where that code which you found came fromI just looked on the python org websiteAnd there appears to be a "getkey" libraryeg.from getkey import getkey, keyskey = getkey()if key == keys.UP:... # Handle the UP keyelif key == keys.DOWN:... # Handle the DOWN keyelse:# Handle other text charactersbuffer += keyprint(buffer)