Python实现跨平台获取按键方法源码示例,python源码,本文是一篇关于Pytho


本文是一篇关于Python语言实现可以跨平台获取按键方法的源码示例文章。python 获取按键需要基于Windows及Unix等不同平台都能实现为最好,如下Python实现跨平台获取按键方法源码供大家参考,如果有需要请参照自己所需要应用的平台做适合的源码修改。

class _Getch:        def __init__(self):        try:            self.impl = _GetchWindows()        except ImportError:            try:                self.impl = _GetchMacCarbon()            except AttributeError:                self.impl = _GetchUnix()    def __call__(self): return self.impl()class _GetchUnix:    def __init__(self):        import tty, sys, termios # import termios now or else you'll get the Unix version on the Mac    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 chclass _GetchWindows:    def __init__(self):        import msvcrt    def __call__(self):        import msvcrt        return msvcrt.getch()class _GetchMacCarbon:        def __init__(self):        import Carbon        Carbon.Evt #see if it has this (in Unix, it doesn't)    def __call__(self):        import Carbon        if Carbon.Evt.EventAvail(0x0008)[0]==0: # 0x0008 is the keyDownMask            return ''        else: #www.iplaypy.com            return chr(msg & 0x000000FF)if __name__ == '__main__': # a little test   print 'Press a key'   inkey = _Getch()   import sys   for i in xrange(sys.maxint):      k=inkey()      if k <> '': break   print 'you pressed ',k

编橙之家文章,

评论关闭