Python可跨平台实现获取按键的方法

 更新时间:2015年03月05日 12:18:47   作者:Sephiroth  
这篇文章主要介绍了Python可跨平台实现获取按键的方法,分别针对windows及Unix等不同平台获取按键的技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了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中类和实例如何绑定属性与方法示例详解

    最近在学习python,纯粹是自己的兴趣爱好,然而并没有系统地看python编程书籍,觉得上面描述过于繁琐,在网站找了一些学习的网站,下面这篇文章主要给大家介绍了关于python中类和实例时如何绑定属性与方法的相关资料,需要的朋友可以参考下。
    2017-08-08
  • selenium+headless chrome爬虫的实现示例

    selenium+headless chrome爬虫的实现示例

    这篇文章主要介绍了selenium+headless chrome爬虫的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-01-01
  • Python编程利用科赫曲线实现三维飘雪效果示例过程

    Python编程利用科赫曲线实现三维飘雪效果示例过程

    这篇文章主要介绍了Python编程实现三维飘雪效果示例过程,通过本示例你可以自己做出一个浪漫的雪花飘落效果,有需要的朋友可以借鉴参考下
    2021-10-10
  • python如何使用正则表达式的前向、后向搜索及前向搜索否定模式详解

    python如何使用正则表达式的前向、后向搜索及前向搜索否定模式详解

    这篇文章主要给大家介绍了关于python如何使用正则表达式的前向、后向搜索及前向搜索否定模式的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起看看吧。
    2017-11-11
  • CentOS7下安装python3.6.8的教程详解

    CentOS7下安装python3.6.8的教程详解

    这篇文章主要介绍了CentOS7下安装python3.6.8的教程,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-01-01
  • 用gpu训练好的神经网络,用tensorflow-cpu跑出错的原因及解决方案

    用gpu训练好的神经网络,用tensorflow-cpu跑出错的原因及解决方案

    这篇文章主要介绍了用gpu训练好的神经网络,用tensorflow-cpu跑出错的原因及解决方案,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-03-03
  • 用ReactJS和Python的Flask框架编写留言板的代码示例

    用ReactJS和Python的Flask框架编写留言板的代码示例

    这篇文章主要介绍了用ReactJS和Python的Flask框架编写留言板的代码示例,其他的话用到了MongoDB这个方便使用JavaScript来操作的数据库,需要的朋友可以参考下
    2015-12-12
  • python多线程并发实例及其优化

    python多线程并发实例及其优化

    这篇文章主要介绍了python多线程并发实例及其优化,threading是扩展模块,在thread的基础上进行了封装及改进。所以只需要使用threading这个模块就能完成并发的测试,需要的朋友可以参考下
    2019-06-06
  • python命名空间(namespace)简单介绍

    python命名空间(namespace)简单介绍

    这篇文章主要介绍了python命名空间(namespace)简单介绍,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-08-08
  • Python编写判断真实文件类型工具

    Python编写判断真实文件类型工具

    常在河边走,哪能不湿鞋,网上获取的各种文件后缀真真假假,甚至一不小心就会中招,所以本文就来用Python编写一个判断真实文件类型工具吧
    2025-01-01

最新评论