0
我正在創建一個程序,需要能夠檢測到窗外的鍵盤按下。我將如何能夠做到像下面這樣:檢測鍵盤按下
while True:
if getKey() == w:
#Do a thing
if getKey() == a:
#Do a different thing
我正在創建一個程序,需要能夠檢測到窗外的鍵盤按下。我將如何能夠做到像下面這樣:檢測鍵盤按下
while True:
if getKey() == w:
#Do a thing
if getKey() == a:
#Do a different thing
假設「窗外」,其實就是「控制檯之外」你可以嘗試像下面以關鍵字事件追加到輸出文件。
import win32api
import sys
import pythoncom, pyHook
buffer = ''
def OnKeyboardEvent(event):
if event.Ascii == 5:
sys.exit()
if event.Ascii != 0 or 8:
f = open ('c:\\output.txt', 'a')
keylogs = chr(event.Ascii)
if event.Ascii == 13:
keylogs = keylogs + '\n'
f.write(keylogs)
f.close()
while True:
hm = pyHook.HookManager()
hm.KeyDown = OnKeyboardEvent
hm.HookKeyboard()
pythoncom.PumpMessages()
http://antihackingtutorials.blogspot.de/2012/06/in-this-tutorial-we-will-show-you-how.html
可以使用的getch() –
http://stackoverflow.com/questions/11918999/key-listeners-in-python – Jonas
這個問題還沒有一個anwser。上面的答案只能在命令控制檯中工作,他正在尋找一些能夠在提示之外工作的東西。 – Moondra