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 拷贝对象(深拷贝deepcopy与浅拷贝copy)
Sep 06 Python
python根据路径导入模块的方法
Sep 30 Python
python列表操作之extend和append的区别实例分析
Jul 28 Python
Python实现字典的key和values的交换
Aug 04 Python
浅谈python为什么不需要三目运算符和switch
Jun 17 Python
python定时利用QQ邮件发送天气预报的实例
Nov 17 Python
python3+PyQt5图形项的自定义和交互 python3实现page Designer应用程序
Jul 20 Python
Django用户认证系统如何实现自定义
Nov 12 Python
python 如何对logging日志封装
Dec 02 Python
Python爬虫之Selenium中frame/iframe表单嵌套页面
Dec 04 Python
pycharm + django跨域无提示的解决方法
Dec 06 Python
Python 内置函数速查表一览
Jun 02 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
全国FM电台频率大全 - 3 河北省
2020/03/11 无线电
谈谈PHP语法(3)
2006/10/09 PHP
centos 5.6 升级php到5.3的方法
2011/05/14 PHP
php页面跳转代码 输入网址跳转到你定义的页面
2013/03/28 PHP
PHP连接MySQL查询结果中文显示乱码解决方法
2013/10/25 PHP
Php连接及读取和写入mysql数据库的常用代码
2014/08/11 PHP
php使用函数pathinfo()、parse_url()和basename()解析URL
2016/11/25 PHP
PHP 中 var_export、print_r、var_dump 调试中的区别
2018/06/19 PHP
Laravel重定向,a链接跳转,控制器跳转示例
2019/10/22 PHP
ExtJs的Date格式字符代码
2010/12/30 Javascript
IE下双击checkbox反应延迟问题的解决方法
2014/03/27 Javascript
Js Jquery创建一个弹出层可加载一个页面
2014/05/08 Javascript
JavaScript中的原型链prototype介绍
2014/12/30 Javascript
微信小程序 实例应用(记账)详解
2016/09/28 Javascript
JavaScript切换搜索引擎的导航网页搜索框实例代码
2017/06/11 Javascript
Django使用多数据库的方法
2017/09/06 Javascript
Vue-cli配置打包文件本地使用的教程图解
2018/08/02 Javascript
Vue+Express实现登录注销功能的实例代码
2019/05/05 Javascript
[01:04:30]Fnatic vs Mineski 2018国际邀请赛小组赛BO2 第二场 8.17
2018/08/18 DOTA
[01:04:49]KG vs LGD 2019国际邀请赛小组赛 BO2 第二场 8.15
2019/08/16 DOTA
python 运算符 供重载参考
2009/06/11 Python
Python抓取京东图书评论数据
2014/08/31 Python
Python时间的精准正则匹配方法分析
2017/08/17 Python
Python实现的查询mysql数据库并通过邮件发送信息功能
2018/05/17 Python
Python2与Python3的区别实例分析
2019/04/11 Python
python跨文件使用全局变量的实现
2020/11/17 Python
俄罗斯香水和化妆品购物网站:Л’Этуаль
2018/05/10 全球购物
随机分配座位,共50个学生,使学号相邻的同学座位不能相邻
2014/01/18 面试题
企业演讲稿范文
2013/12/28 职场文书
报纸媒体创意广告词
2014/03/17 职场文书
总经理任命书
2014/03/29 职场文书
节能减排倡议书
2014/04/15 职场文书
毕业论文致谢词
2015/05/14 职场文书
贫困生证明范文
2015/06/16 职场文书
2016教师学习教育法心得体会
2016/01/19 职场文书
写一个Python脚本自动爬取Bilibili小视频
2021/04/24 Python