Python可跨平臺(tái)實(shí)現(xiàn)獲取按鍵的方法
本文實(shí)例講述了Python可跨平臺(tái)實(shí)現(xiàn)獲取按鍵的方法。分享給大家供大家參考。具體如下:
"""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
希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。
- Python 實(shí)現(xiàn)鍵盤鼠標(biāo)按鍵模擬
- python實(shí)現(xiàn)模擬按鍵,自動(dòng)翻頁(yè)看u17漫畫
- Python實(shí)現(xiàn)windows下模擬按鍵和鼠標(biāo)點(diǎn)擊的方法
- python實(shí)現(xiàn)按鍵精靈找色點(diǎn)擊功能教程,使用pywin32和Pillow庫(kù)
- python中字典按鍵或鍵值排序的實(shí)現(xiàn)代碼
- python按鍵按住不放持續(xù)響應(yīng)的實(shí)例代碼
- python對(duì)綁定事件的鼠標(biāo)、按鍵的判斷實(shí)例
- Python中按鍵來(lái)獲取指定的值
- Python實(shí)現(xiàn)的字典排序操作示例【按鍵名key與鍵值value排序】
- Python實(shí)現(xiàn)對(duì)字典分別按鍵(key)和值(value)進(jìn)行排序的方法分析
- python 字典(dict)按鍵和值排序
- Python 隨機(jī)按鍵模擬2小時(shí)
相關(guān)文章
Python中實(shí)現(xiàn)switch功能實(shí)例解析
這篇文章主要介紹了Python中實(shí)現(xiàn)switch功能實(shí)例解析,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01Python+django實(shí)現(xiàn)簡(jiǎn)單的文件上傳
這篇文章主要為大家詳細(xì)介紹了Python+django實(shí)現(xiàn)簡(jiǎn)單的文件上傳的相關(guān)代碼,感興趣的小伙伴們可以參考一下2016-08-08selenium自動(dòng)化測(cè)試簡(jiǎn)單準(zhǔn)備
本文主要介紹了selenium自動(dòng)化測(cè)試簡(jiǎn)單準(zhǔn)備,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01Python繪圖系統(tǒng)之散點(diǎn)圖和條形圖的實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了如何使用Python繪制散點(diǎn)圖和條形圖,文中的示例代碼講解詳細(xì),對(duì)我們的學(xué)習(xí)或工作有一定的幫助,感興趣的可以了解一下2023-08-08python3實(shí)現(xiàn)windows下同名進(jìn)程監(jiān)控
這篇文章主要為大家詳細(xì)介紹了python3實(shí)現(xiàn)windows下同名進(jìn)程監(jiān)控,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06Python實(shí)現(xiàn)像awk一樣分割字符串
這篇文章主要介紹了Python實(shí)現(xiàn)像awk一樣分割字符串,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09Django項(xiàng)目定期自動(dòng)清除過(guò)期session的2種方法實(shí)例
如果用戶主動(dòng)退出,session會(huì)自動(dòng)清除,如果沒有退出就一直保留,記錄數(shù)越來(lái)越大,要定時(shí)清理沒用的session,下面這篇文章主要給大家介紹了關(guān)于Django項(xiàng)目定期自動(dòng)清除過(guò)期session的2種方法,需要的朋友可以參考下2022-08-08python命令行參數(shù)解析OptionParser類用法實(shí)例
這篇文章主要介紹了python命令行參數(shù)解析OptionParser類用法實(shí)例,需要的朋友可以參考下2014-10-10