学习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-跨平台获取键盘事件的更多相关文章
- C/C++ 获取键盘事件
Windows 系统下的 vs 中可以使用 _kbhit() 函数来获取键盘事件,使用时需要加入 conio.h 头文件 #include <conio.h> #include <i ...
- Python - selenium_WebDriver 鼠标键盘事件
from selenium import webdriver #引入ActionChains类 提供了鼠标的操作方法 from selenium.webdriver.common.action_cha ...
- js中获取键盘事件【转】
<script type="text/javascript" language=JavaScript charset="UTF-8"> 2 docu ...
- js中获取键盘事件
<script type="text/javascript" language=JavaScript charset="UTF-8"> docume ...
- JS获取键盘事件
<script type="text/javascript" language=JavaScript charset="UTF-8"> docume ...
- selenium+Python(鼠标和键盘事件)
本篇总结了 web 页面常用的一些操作元素方法,可以统称为行为事件有些 web 界面的选项菜单需要鼠标悬停在某个元素上才能显示出来(如百度页面的设置按钮). 1 简单操作 1.点击(鼠标左键)页面按钮 ...
- java获取键盘事件
转 <script type="text/javascript" language=JavaScript charset="UTF-8"> docu ...
- JavaScript获取键盘事件
JavaScript中: onkeydown 事件会在用户按下一个键盘按键时发生. onkeypress 事件会在键盘按键被按下并释放一个键时发生. onkeyup 事件会在键盘按键被松开时发生. 支 ...
- python 跨平台获取网卡信息和本机ip地址
笔者在项目中遇到过获取本机网卡ip的例子,利用python库psutil解决了此问题. def get_netcard(): """获取网卡名称和ip地址 "& ...
随机推荐
- Linux bind-utils
一.简介 DNS是一种将域名解析为IP地址的服务.如:www.turbolinux.com.cn通过DNS解析,可以得到210.77.38.126.bind是linux系统下的一个DNS服务程序.bi ...
- sencha表单入门例子
来自于<sencha touch 权威指南> ------------------------------- 一.网站结构 二.index.html代码 <!DOCTYPE HTML ...
- 关于wamp中升级PHP+Apache 的问题
首先个人不建议wamp中升级php版本,如果你不信可以试一试,当你php升级后发想,奥,Apache版本不匹配,然后又去升级Apache,结果搞了半天,弄出来了就好,要是没出来,可能你会气死(好吧,气 ...
- db2中临时表在存储过程中的使用
DROP PROCEDURE ADMINISTRATOR.SP_TEST (INTEGER, CHARACTER ()); CREATE PROCEDURE administrator.sp_test ...
- 修改vscode caipeiyu.writeCnblog ,简化博客发布
修改vscode caipeiyu.writeCnblog ,简化博客发布 1. 安装caipeiyu.writeCnblog vscode的博客园文章发布插件WriteCnblog : https: ...
- MongoDB 深入学习 -- ReplSet,Sharding,Security,Aggregation,Command
萌新最近在对付MongoDB,因此每天都在翻官方文档,这里随便做点笔记 ReplSet 与 Sharding ReplSet 是副本集,也就是主从集合.可以用来做负载均衡,数据热备份.副本集的配置相对 ...
- EF进阶篇(一)——概述
前言 以前在ITOO里面和图书馆项目开发的时候,采用的这个技术,但是总是对上下文那里不是特别清楚.上下文这个概念很是模糊,所以这次再次拿起这个技术点儿,然后复习了一遍,发现我以前想的好简单. 内容 E ...
- winform发布桌面程序后提示需开启“目录浏览”
把发布文件里的publish.htm名字改为index.htm就好了
- 【bzoj5093】 [Lydsy1711月赛]图的价值 组合数+斯特林数+NTT
Description "简单无向图"是指无重边.无自环的无向图(不一定连通). 一个带标号的图的价值定义为每个点度数的k次方的和. 给定n和k,请计算所有n个点的带标号的简单无向 ...
- 题解 P1339 【[USACO09OCT]热浪Heat Wave】
题目链接 这道题纯属是一个裸的SPFA: 建议先把模板AC之后再做. 只需要做一些手脚,就是在加边的时候加一个双向边就好. 然后再第一次加点的时候 看不懂模板的出门左转度娘. 推荐下面一片讲解: 友链 ...