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的Flask框架中@app.route的用法教程
Mar 31 Python
初步解析Python下的多进程编程
Apr 28 Python
Python中常见的数据类型小结
Aug 29 Python
Python装饰器用法示例小结
Feb 11 Python
在python下读取并展示raw格式的图片实例
Jan 24 Python
Python模拟百度自动输入搜索功能的实例
Feb 14 Python
python正则-re的用法详解
Jul 28 Python
opencv调整图像亮度对比度的示例代码
Sep 27 Python
Python2.7:使用Pyhook模块监听鼠标键盘事件-获取坐标实例
Mar 14 Python
Python3标准库之dbm UNIX键-值数据库问题
Mar 24 Python
如何用python处理excel表格
Jun 09 Python
python读取excel数据绘制简单曲线图的完整步骤记录
Oct 30 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
css把超出的部分显示为省略号的方法兼容火狐
2008/07/23 Javascript
javascript 函数调用规则
2009/08/26 Javascript
Javascript保存网页为图片借助于html2canvas库实现
2014/09/05 Javascript
在css加载完毕后自动判断页面是否加入css或js文件
2014/09/10 Javascript
jstree单选功能的实现方法
2017/06/07 Javascript
ubuntu编译nodejs所需的软件并安装
2017/09/12 NodeJs
基于vue 动态加载图片src的解决方法
2018/02/05 Javascript
微信小程序实现换肤功能
2018/03/14 Javascript
React如何避免重渲染
2018/04/10 Javascript
微信小程序scroll-view实现滚动穿透和阻止滚动的方法
2018/08/20 Javascript
实例分析Array.from(arr)与[...arr]到底有何不同
2019/04/09 Javascript
微信小程序常见页面跳转操作简单示例
2019/05/01 Javascript
原生js生成图片验证码
2020/10/11 Javascript
vue3.0 加载json的方法(非ajax)
2020/10/26 Javascript
python使用自定义user-agent抓取网页的方法
2015/04/15 Python
python使用arp欺骗伪造网关的方法
2015/04/24 Python
谈谈Python进行验证码识别的一些想法
2016/01/25 Python
socket + select 完成伪并发操作的实例
2017/08/15 Python
Django中login_required装饰器的深入介绍
2017/11/24 Python
Python使用matplotlib模块绘制图像并设置标题与坐标轴等信息示例
2018/05/04 Python
python中多层嵌套列表的拆分方法
2018/07/02 Python
Python 处理日期时间的Arrow库使用
2020/08/18 Python
Pyinstaller打包Scrapy项目的实现步骤
2020/09/22 Python
ivx平台开发之不用代码实现一个九宫格抽奖功能
2021/01/27 HTML / CSS
.NET程序员的数据库面试题
2012/10/10 面试题
医学生临床实习自我评价
2014/03/07 职场文书
珍惜资源的建议书
2014/08/26 职场文书
2014年党员加强作风建设思想汇报
2014/09/15 职场文书
表扬稿格式范文
2015/01/16 职场文书
全陪导游词
2015/02/04 职场文书
校长师德表现自我评价
2015/03/05 职场文书
房地产公司财务总监岗位职责
2015/04/03 职场文书
python基于tkinter制作下班倒计时工具
2021/04/28 Python
Spring Cloud 中@FeignClient注解中的contextId属性详解
2021/09/25 Java/Android
升级 Win11 还是坚守 Win10?微软 Win11 新系统缺失功能大盘点
2022/04/05 数码科技
nginx之queue的具体使用
2022/06/28 Servers