pyHook不允许我打字

时间:2017-07-29 23:22:28

标签: python selenium keyevent pyhook

我正在尝试在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()

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

问候

1 个答案:

答案 0 :(得分:0)

您将在0中返回OnKeyboardEvent,因此键盘事件不会传递给其他处理程序或窗口本身。如果您不想过滤该事件,则应返回True

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