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  连接字符串(join %)
Sep 06 Python
使用Protocol Buffers的C语言拓展提速Python程序的示例
Apr 16 Python
Python利用带权重随机数解决抽奖和游戏爆装备问题
Jun 16 Python
Python整型运算之布尔型、标准整型、长整型操作示例
Jul 21 Python
Python机器学习之SVM支持向量机
Dec 27 Python
使用python爬虫实现网络股票信息爬取的demo
Jan 05 Python
Python实现查找最小的k个数示例【两种解法】
Jan 08 Python
Python数据可视化库seaborn的使用总结
Jan 15 Python
python实现微信机器人: 登录微信、消息接收、自动回复功能
Apr 29 Python
通过PYTHON来实现图像分割详解
Jun 26 Python
Python装饰器使用你可能不知道的几种姿势
Oct 25 Python
Python排序算法之插入排序及其优化方案详解
Jun 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
为查询结果建立向后/向前按钮
2006/10/09 PHP
PHPLog php 程序调试追踪工具
2009/09/09 PHP
回帖脱衣服的图片实现代码
2014/02/15 PHP
ThinkPHP权限认证Auth实例详解
2014/07/22 PHP
PHP在线打包下载功能示例
2016/10/15 PHP
php+Memcached实现简单留言板功能示例
2017/02/15 PHP
php+mysql+jquery实现日历签到功能
2017/02/27 PHP
PHP多个图片压缩成ZIP的方法
2020/08/18 PHP
JavaScript的eval JSON object问题
2009/11/15 Javascript
window.js 主要包含了页面的一些操作
2009/12/23 Javascript
使用jquery与图片美化checkbox和radio控件的代码(打包下载)
2010/11/11 Javascript
深入理解JavaScript系列(6) 强大的原型和原型链
2012/01/15 Javascript
Shell脚本实现Linux系统和进程资源监控
2015/03/05 Javascript
详解JavaScript的while循环的使用
2015/06/03 Javascript
原生js实现3D轮播图
2020/03/21 Javascript
[44:43]完美世界DOTA2联赛决赛日 FTD vs GXR 第一场 11.08
2020/11/11 DOTA
[51:39]DOTA2-DPC中国联赛 正赛 Magma vs LBZS BO3 第二场 2月7日
2021/03/11 DOTA
python实现sublime3的less编译插件示例
2014/04/27 Python
Python中Collection的使用小技巧
2014/08/18 Python
Python科学计算之Pandas详解
2017/01/15 Python
Python中生成Epoch的方法
2017/04/26 Python
python基础之入门必看操作
2017/07/26 Python
python3+PyQt5自定义视图详解
2018/04/24 Python
关于Pycharm无法debug问题的总结
2019/01/19 Python
详解Python计算机视觉 图像扭曲(仿射扭曲)
2019/03/27 Python
Python实现疫情通定时自动填写功能(附代码)
2020/05/27 Python
HTML5标签嵌套规则详解【必看】
2016/04/26 HTML / CSS
乐天旅游台湾网站:Rakuten Travel TW
2017/06/01 全球购物
美国最灵活的移动提供商:Tello
2017/07/18 全球购物
美国最大的在线生存商店:Survival Frog
2020/12/13 全球购物
linux下进程间通信的方式
2013/01/23 面试题
本科生就业推荐信
2014/05/19 职场文书
张家口市高新区党工委群众路线教育实践活动整改方案
2014/10/25 职场文书
劳动保障个人工作总结
2015/03/04 职场文书
小学中队委竞选稿
2015/11/20 职场文书
2019年暑期安全广播稿!
2019/07/03 职场文书