我正在尝试检测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()也许有人能告诉我怎么修?
问候
发布于 2017-07-30 13:28:53
您将在OnKeyboardEvent中返回0,因此键盘事件不会传递给其他处理程序或窗口本身。如果你不想过滤事件,你应该返回True。
有关详细信息,请参阅文档中的Event Filtering。
https://stackoverflow.com/questions/45394940
复制相似问题