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实现拼接多张图片的方法
Dec 01 Python
Python查找函数f(x)=0根的解决方法
May 07 Python
python获取指定目录下所有文件名列表的方法
May 20 Python
Python实现的简单dns查询功能示例
May 24 Python
Python有序字典简单实现方法示例
Sep 28 Python
Tensorflow实现AlexNet卷积神经网络及运算时间评测
May 24 Python
python 保存float类型的小数的位数方法
Oct 17 Python
对Python中Iterator和Iterable的区别详解
Oct 18 Python
Django shell调试models输出的SQL语句方法
Aug 29 Python
python多线程同步之文件读写控制
Feb 25 Python
python实现高斯(Gauss)迭代法的例子
Nov 20 Python
Python Celery异步任务队列使用方法解析
Aug 10 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
PHP 自定义错误处理函数的使用详解
2013/05/10 PHP
用PHP代码在网页上生成图片
2015/07/01 PHP
编写PHP脚本来实现WordPress中评论分页的功能
2015/12/10 PHP
php cookie用户登录的详解及实例代码
2017/01/03 PHP
laravel-admin自动生成模块,及相关基础配置方法
2019/10/08 PHP
php的instanceof和判断闭包Closure操作示例
2020/01/26 PHP
ThinkPHP5与单元测试PHPUnit使用详解
2020/02/23 PHP
JS代码判断IE6,IE7,IE8,IE9的函数代码
2013/08/02 Javascript
textarea 控制输入字符字节数(示例代码)
2013/12/27 Javascript
基于jquery实现的可编辑下拉框实现代码
2014/08/02 Javascript
javascript表格的渲染组件
2015/07/03 Javascript
JS与jQuery遍历Table所有单元格内容的方法
2015/12/07 Javascript
js简单设置与使用cookie的方法
2016/01/22 Javascript
使用CSS+JavaScript或纯js实现半透明遮罩效果的实例分享
2016/05/09 Javascript
详解es6超好用的语法糖Decorator
2018/08/01 Javascript
jQuery实现鼠标移到某个对象时弹出显示层功能
2018/08/23 jQuery
JavaScript对象原型链原理详解
2020/02/05 Javascript
vue引入静态js文件的方法
2020/06/20 Javascript
django 常用orm操作详解
2017/09/13 Python
python列表的增删改查实例代码
2018/01/30 Python
python爬虫之模拟登陆csdn的实例代码
2018/05/18 Python
Python分支语句与循环语句应用实例分析
2019/05/07 Python
pygame编写音乐播放器的实现代码示例
2019/11/19 Python
python-web根据元素属性进行定位的方法
2019/12/13 Python
tensorflow将图片保存为tfrecord和tfrecord的读取方式
2020/02/17 Python
PyQt5.6+pycharm配置以及pyinstaller生成exe(小白教程)
2020/06/02 Python
世界首屈一指的在线男士内衣权威:HisRoom
2017/08/05 全球购物
自荐信包含哪些内容
2013/10/30 职场文书
葡萄牙语专业个人求职信
2013/12/10 职场文书
捐书倡议书
2014/08/29 职场文书
上班离岗检讨书
2014/09/10 职场文书
2015年初三班主任工作总结
2015/05/21 职场文书
2016年国庆节宣传标语
2015/11/25 职场文书
2016年度优秀辅导员事迹材料
2016/02/26 职场文书
MySQL不使用order by实现排名的三种思路总结
2021/06/02 MySQL
Html5获取用户当前位置的几种方式
2022/01/18 HTML / CSS