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启动办公软件进程(word、excel、ppt、以及wps的et、wps、wpp)
Apr 09 Python
黑科技 Python脚本帮你找出微信上删除你好友的人
Jan 07 Python
Python实现脚本锁功能(同时只能执行一个脚本)
May 10 Python
python自定义异常实例详解
Jul 11 Python
python虚拟环境的安装配置图文教程
Oct 20 Python
Python堆排序原理与实现方法详解
May 11 Python
利用pandas将numpy数组导出生成excel的实例
Jun 14 Python
python pyinstaller 加载ui路径方法
Jun 10 Python
Python+selenium点击网页上指定坐标的实例
Jul 05 Python
django 自定义过滤器(filter)处理较为复杂的变量方法
Aug 12 Python
python实现高斯(Gauss)迭代法的例子
Nov 20 Python
利用Python中的Xpath实现一个在线汇率转换器
Sep 09 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版
2016/07/22 PHP
Aster vs KG BO3 第三场2.19
2021/03/10 DOTA
使用TextRange获取输入框中光标的位
2006/10/14 Javascript
JavaScript控制网页平滑滚动到指定元素位置的方法
2015/04/17 Javascript
js正则表达式中exec用法实例
2015/07/23 Javascript
JQuery日历插件My97DatePicker日期范围限制
2016/01/20 Javascript
基于BootStrap Metronic开发框架经验小结【七】数据的导入、导出及附件的查看处理
2016/05/12 Javascript
jquery mobile 实现自定义confirm确认框效果的简单实例
2016/06/17 Javascript
jQuery鼠标事件总结
2016/10/13 Javascript
AngularJS实现在ng-Options加上index的解决方法
2016/11/03 Javascript
浅谈js函数中的实例对象、类对象、局部变量(局部函数)
2016/11/20 Javascript
原生js 封装get ,post, delete 请求的实例
2017/08/11 Javascript
解决IE7中使用jQuery动态操作name问题
2017/08/28 jQuery
基于Node.js实现压缩和解压缩的方法
2018/02/13 Javascript
vue实现提示保存后退出的方法
2018/03/15 Javascript
微信小程序中进行地图导航功能的实现方法
2018/06/29 Javascript
老生常谈JS中的继承及实现代码
2018/07/06 Javascript
jQuery分组选择器简单用法示例
2019/04/04 jQuery
使用pandas中的DataFrame数据绘制柱状图的方法
2018/04/10 Python
Python用Try语句捕获异常的实例方法
2019/06/26 Python
详解利用OpenCV提取图像中的矩形区域(PPT屏幕等)
2019/07/01 Python
python笔记之mean()函数实现求取均值的功能代码
2019/07/05 Python
Python Numpy计算各类距离的方法
2019/07/05 Python
解决Django layui {{}}冲突的问题
2019/08/29 Python
Python爬虫爬取Bilibili弹幕过程解析
2019/10/10 Python
Python 脚本的三种执行方式小结
2019/12/21 Python
HTML5 FileReader对象的具体使用方法
2020/05/22 HTML / CSS
拉斯维加斯城市观光通行证:Las Vegas Pass
2019/05/21 全球购物
海量信息软件测试笔试题
2015/08/08 面试题
大专生自荐信
2013/10/04 职场文书
档案接收函
2014/01/13 职场文书
大学生冰淇淋店商业计划书
2014/01/14 职场文书
教师节慰问信
2015/02/15 职场文书
公司清洁工岗位职责
2015/04/15 职场文书
停课通知书
2015/04/24 职场文书
Nginx内网单机反向代理的实现
2021/11/07 Servers