pyHook不让我打字



我正在尝试检测python中的全局按键。(我是一个完全的Python菜鸟(。我的问题是 pyHook 识别我的关键事件,但它不再让我输入。如果我尝试在打开的硒网络驱动程序中键入一些内容(请参阅代码(,除了打印 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中返回0,因此键盘事件不会传递给其他处理程序或窗口本身。如果不想筛选事件,则应返回True

有关详细信息,请参阅文档中的事件过滤。

最新更新