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


Posted in Python onMarch 05, 2015

本文实例讲述了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程序设计有所帮助。

Python 相关文章推荐
python简单线程和协程学习心得(分享)
Jun 14 Python
使用Python的package机制如何简化utils包设计详解
Dec 11 Python
python 顺时针打印矩阵的超简洁代码
Nov 14 Python
python 检查文件mime类型的方法
Dec 08 Python
python实现浪漫的烟花秀
Jan 30 Python
详解python列表生成式和列表生成式器区别
Mar 27 Python
Django时区详解
Jul 24 Python
Python CVXOPT模块安装及使用解析
Aug 01 Python
python GUI库图形界面开发之PyQt5信号与槽基础使用方法与实例
Mar 06 Python
Python dict的常用方法示例代码
Jun 23 Python
python关于集合的知识案例详解
May 30 Python
python函数的两种嵌套方法使用
Apr 02 Python
Python读取mp3中ID3信息的方法
Mar 05 #Python
Python查找相似单词的方法
Mar 05 #Python
Python兔子毒药问题实例分析
Mar 05 #Python
Python获取服务器信息的最简单实现方法
Mar 05 #Python
Python实现简单的可逆加密程序实例
Mar 05 #Python
Python装饰器的函数式编程详解
Feb 27 #Python
python分析nignx访问日志脚本分享
Feb 26 #Python
You might like
PHP5 操作MySQL数据库基础代码
2009/09/29 PHP
phpmailer中文乱码问题的解决方法
2014/04/22 PHP
php中addslashes函数与sql防注入
2014/11/17 PHP
学习php设计模式 php实现观察者模式(Observer)
2015/12/09 PHP
基于jQuery的Spin Button自定义文本框数值自增或自减
2010/07/17 Javascript
javascript按位非运算符的使用方法
2013/11/14 Javascript
js文件包含的几种方式介绍
2014/09/28 Javascript
JS折半插入排序算法实例
2015/12/02 Javascript
canvas仿iwatch时钟效果
2017/03/06 Javascript
vue-cli2.9.3 详细教程
2018/04/23 Javascript
浅析Proxy可以优化vue的数据监听机制问题及实现思路
2018/11/29 Javascript
推荐一个基于Node.js的表单验证库
2019/02/15 Javascript
JavaScript解析JSON数据示例
2019/07/16 Javascript
Vue3新特性之在Composition API中使用CSS Modules
2020/07/13 Javascript
JavaScript字符串转数字的简单实现方法
2020/11/27 Javascript
Python time模块详解(常用函数实例讲解,非常好)
2014/04/24 Python
从零学Python之入门(三)序列
2014/05/25 Python
Python的Twisted框架中使用Deferred对象来管理回调函数
2016/05/25 Python
浅谈Python中带_的变量或函数命名
2017/12/04 Python
Python自定义线程池实现方法分析
2018/02/07 Python
python实现梯度下降算法
2020/03/24 Python
对python PLT中的image和skimage处理图片方法详解
2019/01/10 Python
python3.6+selenium实现操作Frame中的页面元素
2019/07/16 Python
python实现高斯(Gauss)迭代法的例子
2019/11/20 Python
德国大型的家具商店:Pharao24.de
2016/10/02 全球购物
美国知名女性服饰品牌:New York & Company
2017/03/23 全球购物
美国在线纱线商店:Darn Good Yarn
2019/03/20 全球购物
德国最大的服装、鞋子和配件在线商店之一:Outfits24
2019/07/23 全球购物
大码女装:Ulla Popken
2019/08/06 全球购物
kfc实习自我鉴定
2013/12/14 职场文书
《欢乐的泼水节》教学反思
2014/04/22 职场文书
建筑工程技术专业求职信
2014/07/16 职场文书
2014年财务经理工作总结
2014/12/08 职场文书
2015年药店店长工作总结
2015/04/29 职场文书
python自动统计zabbix系统监控覆盖率的示例代码
2021/04/03 Python
vue-element-admin项目导入和导出的实现
2021/05/21 Vue.js