Python key binding/capture -
i'd know simplest way bind keys in python
for example, default python console window apears , waits, in psuedo ->
if key "y" pressed: print ("yes") if key "n" pressed: print ("no")
i achieve without use of modules not included python. pure python
any , appreciated
python 2.7 or 3.x windows 7
note: raw_input()
requires user hit enter , therefore not keybinding
from http://code.activestate.com/recipes/134892/ (although bit simplified):
class _getch: """gets single character standard input. not echo screen.""" def __init__(self): self.impl = _getchunix() def __call__(self): return self.impl() class _getchunix: def __init__(self): import tty, sys def __call__(self): import sys, 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 getch = _getch()
then can do:
>>> getch() 'y' # here typed y
this great doesn't need 3rd party modules.
Comments
Post a Comment