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 <http://rosettacode.org/wiki/Category:Python>
#!/usr/bin/env python import __future__import sysif sys.version_info.major < 3: import thread as _threadelse: import _threadimport time
try: from msvcrt import getch # try to import Windows versionexcept 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 <http://rosettacode.org/wiki/Category: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 too Never having tried this in python, so I had to look this up not sure where that code which you found came from I just looked on the python org website And there appears to be a "getkey" library https://pypi.python.org/pypi/getkey/0.6.5 eg. from getkey import getkey, keys key = getkey() if key == keys.UP: ... # Handle the UP key elif key == keys.DOWN: ... # Handle the DOWN key else: # Handle other text characters buffer += key print(buffer)
On Sun, Feb 19, 2017 at 4:38 PM, Douglas Crawford <touchetek@gmail.com> wrote
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.
It's common with most other languages to use libraries, going as far back to Fortran, not sure which used it before that. I think it's still considered "built-in" even though the libraries are linked during compile time And you only link the libraries you need, to keep the code compact. It's a different paradigm with Basic, that never had this mechanism as far as I've seen. Either you had enough commands are you had to live with what they gave you and improvise. The "one-line" command applies to both, because in the end it's still machine code I only had to search this on their website because of the vastness of their library suite. Not like it would fit on those Basic reference cards we grew up on. Dan
participants (2)
-
Dan Roganti -
Douglas Crawford