pyHook 不让我打字

pyHook doesnt let me type

我正在尝试检测 python 中的全局按键。 (我是 Python 中的菜鸟)。我的问题是 pyHook 识别我的关键事件,但它不再让我输入。如果我尝试在打开的 selenium webdriver 中输入一些内容(请参阅代码),除了正在打印的 keyid 之外什么也没有发生。

这是我的代码:

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()

也许有人可以告诉我如何解决它?

问候

您正在 OnKeyboardEvent 中 returning 0,因此键盘事件不会传递给其他处理程序或 window 本身。如果你不想过滤事件,你应该return True.

有关详细信息,请参阅文档中的 Event Filtering