Python实现跨平台获取按键方法源码示例,python源码,本文是一篇关于Pytho
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
编橙之家文章,
相关内容
- Python计算n*n网格生成树数目的方法,python网格,Python计算
- Python实现HTML颜色和RGB颜色互转的方法,pythonrgb,本文是
- Python随机产生迷宫生成算法源码,python算法,用Python语言
- Python方法如何将普通IP转换为十进制IP,python十进制,P
- Python组合生成与数量计算的实现方法,python组合数量计
- Python实现Windows自动切换代理的方法,python自动切换,本
- Python常见排序算法实现与测速源码,python测速,Python常见
- Python统计代码行数的快捷方法,python统计行数,想知道一
- 如何用Python方法获取图片的准确尺寸,,Python如何获取图
- 把Gmail邮件转发到gtalk的Python方法,gtalkpython,用Python方法
评论关闭