• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > Python可跨平台实现获取按键的方法

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

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了python 按键精灵,python 按键,python 模拟按键,python 字典按键排序,python实现单例的方法等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

本文实例讲述了Python可跨平台实现获取按键的方法。分享给大家供大家参考。具体如下:

    """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</div>

希望本文所述对大家的Python程序设计有所帮助。

</div>

您可能想查找下面的文章:

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

相关文章

  • python网络编程之读取网站根目录实例
  • Python接收Gmail新邮件并发送到gtalk的方法
  • Python类的专用方法实例分析
  • Python网络编程中urllib2模块的用法总结
  • python实现dnspod自动更新dns解析的方法
  • python启动办公软件进程(word、excel、ppt、以及wps的et、wps、wpp)
  • 使用python实现rsa算法代码
  • 浅析python 内置字符串处理函数的使用方法
  • 使用Python脚本将文字转换为图片的实例分享
  • Python中使用装饰器时需要注意的一些问题

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • Python字符串转换成浮点数函数分享
    • 对于Python编程中一些重用与缩减的建议
    • Python ORM框架SQLAlchemy学习笔记之安装和简单查询实例
    • python获取指定目录下所有文件名列表的方法
    • python fabric实现远程部署
    • 用Python实现斐波那契(Fibonacci)函数
    • Python使用CMD模块更优雅的运行脚本
    • pyenv命令管理多个Python版本
    • python处理文本文件实现生成指定格式文件的方法
    • Python3基础之函数用法

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有