1

I am trying to detect global keypresses in python. (I am a complete noob in Python). My problem is that pyHook recognizes my key events but it doesn't let me type anymore. If I try to type something into the opened selenium webdriver (see code), nothing happens, except for the keyid being printed.

Here is my code:

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

Maybe someone can tell me how to fix it?

greetings

Adrián Kálazi
  • 250
  • 1
  • 2
  • 13
Sleeyz
  • 25
  • 3

1 Answers1

0

You're are returning 0 in OnKeyboardEvent, so the keyboard event is not passed to other handlers or the window itself. If you don't want to filter the event, you should return True.

See Event Filtering in the documentation for more information.

kichik
  • 33,220
  • 7
  • 94
  • 114