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基于scrapy采集数据时使用代理服务器的方法
Apr 16 Python
python中查看变量内存地址的方法
May 05 Python
Python正则表达式使用经典实例
Jun 21 Python
理解python中生成器用法
Dec 20 Python
Python将图片转换为字符画的方法
Jun 16 Python
解决vscode python print 输出窗口中文乱码的问题
Dec 03 Python
Pandas的read_csv函数参数分析详解
Jul 02 Python
tensorflow -gpu安装方法(不用自己装cuda,cdnn)
Jan 20 Python
python 计算概率密度、累计分布、逆函数的例子
Feb 25 Python
Tensorflow tensor 数学运算和逻辑运算方式
Jun 30 Python
python Paramiko使用示例
Sep 21 Python
PyCharm 配置SSH和SFTP连接远程服务器
May 11 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
DISCUZ 论坛管理员密码忘记的解决方法
2009/05/14 PHP
通过PHP的内置函数,通过DES算法对数据加密和解密
2012/06/21 PHP
对象无length属性时IE6/IE7中无法将其转换成伪数组(ArrayLike)
2011/07/31 Javascript
jquery load事件(callback/data)使用方法及注意事项
2013/02/06 Javascript
高效率JavaScript编写技巧整理
2013/08/23 Javascript
基于编写jQuery的无缝滚动插件
2014/08/02 Javascript
jQuery中outerWidth()方法用法实例
2015/01/19 Javascript
详解JavaScript时间格式化
2015/12/23 Javascript
基于Bootstrap实现图片轮播效果
2016/05/22 Javascript
angular2 ng2 @input和@output理解及示例
2017/10/10 Javascript
微信小程序画布圆形进度条显示效果
2020/11/17 Javascript
通过webpack引入第三方库的方法
2018/07/20 Javascript
解决vue2 在mounted函数无法获取prop中的变量问题
2018/11/15 Javascript
vue中多路由表头吸顶实现的几种布局方式
2019/04/12 Javascript
VUE : vue-cli中去掉路由中的井号#操作
2020/09/04 Javascript
[01:32]DOTA2 2015国际邀请赛中国区预选赛第四日战报
2015/05/29 DOTA
跟老齐学Python之list和str比较
2014/09/20 Python
python 与GO中操作slice,list的方式实例代码
2017/03/20 Python
python编写分类决策树的代码
2017/12/21 Python
python3利用Socket实现通信的方法示例
2019/05/06 Python
django创建超级用户过程解析
2019/09/18 Python
Python+PyQt5实现灭霸响指功能
2020/05/25 Python
django使用channels实现通信的示例
2020/10/19 Python
Python关于拓扑排序知识点讲解
2021/01/04 Python
YSL圣罗兰美妆美国官网:Yves Saint Lauret US
2016/11/21 全球购物
利达恒信公司.NET笔试题面试题
2016/03/05 面试题
大学生标准推荐信范文
2013/11/25 职场文书
大学生秋游活动方案
2014/02/17 职场文书
2014年3.15团委活动总结
2014/03/16 职场文书
上班时间打瞌睡检讨书
2014/09/26 职场文书
2014向国旗敬礼网上签名活动总结
2014/09/27 职场文书
水电工岗位职责
2015/02/14 职场文书
营销策划分析:怎么策划才能更好销量产品?
2019/09/04 职场文书
vue使用节流函数的踩坑实例指南
2021/05/20 Vue.js
OpenCV-Python 实现两张图片自动拼接成全景图
2021/06/11 Python
redis cluster支持pipeline的实现思路
2021/06/23 Redis