2017-07-29 97 views
1

我想在python中檢測全局按鍵。 (我是Python中的完全noob)。我的問題是,pyHook識別我的關鍵事件,但它不會讓我再打字。如果我嘗試在打開的硒驅動程序中輸入某些內容(請參閱代碼),除了要打印的keyid外,沒有任何反應。pyHook doesnt讓我鍵入

這裏是我的代碼:

import pyHook, pythoncom, sys, win32api 
from colorama import Fore, init 
from selenium import webdriver 

add_key = 187 #keyID for "+" key 
commands = ["start", "quit", "save", "help"] 
urls = [] 
driver = webdriver.Chrome() 

def OnKeyboardEvent(event): 
    print(event.KeyID) 
    if event.KeyID == add_key: 
     print("add key pressed") 
     urls.append(driver.current_url) 
    return 0 

def PrintHelpMessage(): 
    # write help message 
    MainLoop() 

def MainLoop(): 
    print(Fore.GREEN + "type commands for more help.") 

    usr_input = input() 
    if usr_input == "commands": 
     print(Fore.GREEN + "available commands: start, quit, save, help") 
     command_input = input() 
     if command_input in commands: 
      if command_input == "start": 
       hook_manager = pyHook.HookManager() 
       hook_manager.KeyDown = OnKeyboardEvent 
       hook_manager.HookKeyboard() 
       pythoncom.PumpMessages() 
      elif command_input == "quit": 
       sys.exit(0) 
      elif command_input == "save": 
       # implement save function 
       print("Save function implemented soon") 
      elif command_input == "help": 
       PrintHelpMessage() 


init(autoreset = True) # init colorama -> makes it possible to use colored text in terminal 
print(Fore.RED + "---easy playlist manager---") 
driver.get("http://youtube.com") 
MainLoop() 

也許有人可以告訴我怎麼解決?

問候

回答

0

你正在重返OnKeyboardEvent0,使鍵盤事件不會傳遞到其他處理程序或窗口本身。如果您不想過濾事件,則應返回True

請參閱Event Filtering的文檔以獲取更多信息。