Python可跨平台实现获取按键的方法

yipeiwu_com5年前Python基础

本文实例讲述了Python可跨平台实现获取按键的方法。分享给大家供大家参考。具体如下:

复制代码 代码如下:
class _Getch: 
    """Gets a single character from standard input.  Does not echo to the screen."""
    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 ch 
class _GetchWindows: 
    def __init__(self): 
        import msvcrt 
    def __call__(self): 
        import msvcrt 
        return msvcrt.getch() 
class _GetchMacCarbon: 
    """ 
    A function which returns the current ASCII key that is down; 
    if no ASCII key is down, the null string is returned.  The 
    page http://www.mactech.com/macintosh-c/chap02-1.html was 
    very helpful in figuring out how to do this. 
    """
    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: 
            # 
            # The event contains the following info: 
            # (what,msg,when,where,mod)=Carbon.Evt.GetNextEvent(0x0008)[1] 
            # 
            # The message (msg) contains the ASCII char which is 
            # extracted with the 0x000000FF charCodeMask; this 
            # number is converted to an ASCII character with chr() and 
            # returned 
            # 
            (what,msg,when,where,mod)=Carbon.Evt.GetNextEvent(0x0008)[1] 
            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程序设计有所帮助。

相关文章

初步剖析C语言编程中的结构体

C语言结构体,可谓是C强大功能之一,也是C++语言之所以能衍生的有利条件,事实上,当结构体中成员中有函数指针了后,那么,结构体也即C++中的类了。 C语言中,结构体的声明、定义是用到关键...

执行Django数据迁移时报 1091错误及解决方法

执行Django数据迁移时报 1091错误及解决方法

问题描述   今天在Pycharm 中的Terminal下,执行数据迁移操作时,第一步: Python manage.py makemigrations ,是没有任何问题,但就是在执行真...

深入理解python中的浅拷贝和深拷贝

深入理解python中的浅拷贝和深拷贝

在讲什么是深浅拷贝之前,我们先来看这样一个现象: a = ['scolia', 123, [], ] b = a[:] b[2].append(666) print a print...

python numpy元素的区间查找方法

找了半天,以为numpy的where函数像matlab 的find函数一样好用,能够返回一个区间内的元素索引位置。结果没有。。(也可能是我没找到) 故自己写一个函数,找多维数组下的,在某...

python 数据清洗之数据合并、转换、过滤、排序

python 数据清洗之数据合并、转换、过滤、排序

前面我们用pandas做了一些基本的操作,接下来进一步了解数据的操作, 数据清洗一直是数据分析中极为重要的一个环节。 数据合并 在pandas中可以通过merge对数据进行合并操作。...