Python可跨平臺實現(xiàn)獲取按鍵的方法
更新時間:2015年03月05日 12:18:47 作者:Sephiroth
這篇文章主要介紹了Python可跨平臺實現(xiàn)獲取按鍵的方法,分別針對windows及Unix等不同平臺獲取按鍵的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了Python可跨平臺實現(xiàn)獲取按鍵的方法。分享給大家供大家參考。具體如下:
復制代碼 代碼如下:
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程序設計有所幫助。
您可能感興趣的文章:
- Python 實現(xiàn)鍵盤鼠標按鍵模擬
- python實現(xiàn)模擬按鍵,自動翻頁看u17漫畫
- Python實現(xiàn)windows下模擬按鍵和鼠標點擊的方法
- python實現(xiàn)按鍵精靈找色點擊功能教程,使用pywin32和Pillow庫
- python中字典按鍵或鍵值排序的實現(xiàn)代碼
- python按鍵按住不放持續(xù)響應的實例代碼
- python對綁定事件的鼠標、按鍵的判斷實例
- Python中按鍵來獲取指定的值
- Python實現(xiàn)的字典排序操作示例【按鍵名key與鍵值value排序】
- Python實現(xiàn)對字典分別按鍵(key)和值(value)進行排序的方法分析
- python 字典(dict)按鍵和值排序
- Python 隨機按鍵模擬2小時
相關文章
selenium+headless chrome爬蟲的實現(xiàn)示例
這篇文章主要介紹了selenium+headless chrome爬蟲的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-01-01
Python編程利用科赫曲線實現(xiàn)三維飄雪效果示例過程
這篇文章主要介紹了Python編程實現(xiàn)三維飄雪效果示例過程,通過本示例你可以自己做出一個浪漫的雪花飄落效果,有需要的朋友可以借鑒參考下2021-10-10
python如何使用正則表達式的前向、后向搜索及前向搜索否定模式詳解
這篇文章主要給大家介紹了關于python如何使用正則表達式的前向、后向搜索及前向搜索否定模式的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起看看吧。2017-11-11
用gpu訓練好的神經(jīng)網(wǎng)絡,用tensorflow-cpu跑出錯的原因及解決方案
這篇文章主要介紹了用gpu訓練好的神經(jīng)網(wǎng)絡,用tensorflow-cpu跑出錯的原因及解決方案,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03
用ReactJS和Python的Flask框架編寫留言板的代碼示例
這篇文章主要介紹了用ReactJS和Python的Flask框架編寫留言板的代碼示例,其他的話用到了MongoDB這個方便使用JavaScript來操作的數(shù)據(jù)庫,需要的朋友可以參考下2015-12-12

