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

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -