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-跨平台获取键盘事件的更多相关文章

  1. C/C++ 获取键盘事件

    Windows 系统下的 vs 中可以使用 _kbhit() 函数来获取键盘事件,使用时需要加入 conio.h 头文件 #include <conio.h> #include <i ...

  2. Python - selenium_WebDriver 鼠标键盘事件

    from selenium import webdriver #引入ActionChains类 提供了鼠标的操作方法 from selenium.webdriver.common.action_cha ...

  3. js中获取键盘事件【转】

    <script type="text/javascript" language=JavaScript charset="UTF-8"> 2 docu ...

  4. js中获取键盘事件

    <script type="text/javascript" language=JavaScript charset="UTF-8"> docume ...

  5. JS获取键盘事件

    <script type="text/javascript" language=JavaScript charset="UTF-8"> docume ...

  6. selenium+Python(鼠标和键盘事件)

    本篇总结了 web 页面常用的一些操作元素方法,可以统称为行为事件有些 web 界面的选项菜单需要鼠标悬停在某个元素上才能显示出来(如百度页面的设置按钮). 1 简单操作 1.点击(鼠标左键)页面按钮 ...

  7. java获取键盘事件

    转 <script type="text/javascript" language=JavaScript charset="UTF-8"> docu ...

  8. JavaScript获取键盘事件

    JavaScript中: onkeydown 事件会在用户按下一个键盘按键时发生. onkeypress 事件会在键盘按键被按下并释放一个键时发生. onkeyup 事件会在键盘按键被松开时发生. 支 ...

  9. python 跨平台获取网卡信息和本机ip地址

    笔者在项目中遇到过获取本机网卡ip的例子,利用python库psutil解决了此问题. def get_netcard(): """获取网卡名称和ip地址 "& ...

随机推荐

  1. 12.Alias(别名)

    通过使用 SQL,可以为列名称和表名称指定别名(Alias). SQL Alias 表的 SQL Alias 语法 SELECT column_name(s) FROM table_name AS a ...

  2. Luogu 1379 八数码难题

    吐槽:此题就是一点一点卡过去的 警告: 1.千万不能用dfs搜这种东西(dfs需要遍历所有状态才能找到最优解), 分分钟爆炸 2.写结构体的时候要综合判断&的加和不加 Code: // luo ...

  3. 权限管理RBAC

    四张表: 1.module:id/name //模块 2.action:id /module_id/name //权限 3.user:id/name //用户表 4.group:id/user_id ...

  4. Smarty3——内置函数

    Table of Content {$var} {$append} {assign} {block} {call} {config_load} {debug} {extends} {for} {for ...

  5. 实践作业3:白盒测试----findbugs介绍及使用DAY7

    本小组选择的是一个开源的Java静态代码分析工具----Findbugs. 与其他静态分析工具(如Checkstyle和PMD)不同,FindBugs 不注重样式或者格式,它专注于寻找真正的缺陷或者潜 ...

  6. Word文件乱码XML

    文章介绍 一个朋友写的文档因为异常关机,导致全部文件变成了xml的乱码,正好帮他解决了,感觉这些或许有些帮助,就先记录下来了. 破损文件介绍 文件破坏之后,打开全是xml格式的文档,结构如下. 恢复过 ...

  7. Daubechies Wavelet

    The Daubechies wavelets, based on the work of Ingrid Daubechies, are a family of orthogonal wavelets ...

  8. 拉登是我罩的队_第三周_需求改进&原型设计

    需求改进&原型设计 1. 需求&原型改进 1.1硬件部分(1) 硬件部分分为主机和遥控器,分别由两个单片机进行控制.(2) 单片机1:负责显示游戏的界面.使用数码管显示当前得分,使用不 ...

  9. MongoDB整理笔记のGUI操作

    值得幸运的是,其实MongoDB也有像类似于PL/SQL一样的界面操作工具操作MongoDB. 下面就来介绍几款不同的界面工具,大家各取所需! MongoVUE 主页:http://www.mongo ...

  10. linux 系统的ssh服务

    ssh服务由服务端软件Openssh和客户端(常见的有ssh,SecureCRT,putty,xshell)组成,ssh服务默认使用22端口提供服务,它有两个不兼容的ssh协议版本,分别是1.x和2. ...