Python可跨平臺實現(xiàn)獲取按鍵的方法
更新時間:2015年03月05日 12:18:47 作者:Sephiroth
這篇文章主要介紹了Python可跨平臺實現(xiàn)獲取按鍵的方法,分別針對windows及Unix等不同平臺獲取按鍵的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了Python可跨平臺實現(xiàn)獲取按鍵的方法。分享給大家供大家參考。具體如下:
復(fù)制代碼 代碼如下:
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
"""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程序設(shè)計有所幫助。
您可能感興趣的文章:
- Python 實現(xiàn)鍵盤鼠標(biāo)按鍵模擬
- python實現(xiàn)模擬按鍵,自動翻頁看u17漫畫
- Python實現(xiàn)windows下模擬按鍵和鼠標(biāo)點擊的方法
- python實現(xiàn)按鍵精靈找色點擊功能教程,使用pywin32和Pillow庫
- python中字典按鍵或鍵值排序的實現(xiàn)代碼
- python按鍵按住不放持續(xù)響應(yīng)的實例代碼
- python對綁定事件的鼠標(biāo)、按鍵的判斷實例
- Python中按鍵來獲取指定的值
- Python實現(xiàn)的字典排序操作示例【按鍵名key與鍵值value排序】
- Python實現(xiàn)對字典分別按鍵(key)和值(value)進(jìn)行排序的方法分析
- python 字典(dict)按鍵和值排序
- Python 隨機(jī)按鍵模擬2小時
相關(guān)文章
Python繪圖系統(tǒng)之散點圖和條形圖的實現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了如何使用Python繪制散點圖和條形圖,文中的示例代碼講解詳細(xì),對我們的學(xué)習(xí)或工作有一定的幫助,感興趣的可以了解一下2023-08-08python3實現(xiàn)windows下同名進(jìn)程監(jiān)控
這篇文章主要為大家詳細(xì)介紹了python3實現(xiàn)windows下同名進(jìn)程監(jiān)控,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-06-06Django項目定期自動清除過期session的2種方法實例
如果用戶主動退出,session會自動清除,如果沒有退出就一直保留,記錄數(shù)越來越大,要定時清理沒用的session,下面這篇文章主要給大家介紹了關(guān)于Django項目定期自動清除過期session的2種方法,需要的朋友可以參考下2022-08-08python命令行參數(shù)解析OptionParser類用法實例
這篇文章主要介紹了python命令行參數(shù)解析OptionParser類用法實例,需要的朋友可以參考下2014-10-10