Thursday 26 November 2020

Pynput listener for autocomplete / hotkey typing

I am attempting to write a simple autocomplete / hotkeys script that will allow me to type things like SHIFT + K, which Python and Pynput will convert to "Kind regards, John Smith, Sales Manager". The following code types the text an infinite number of times and crashes the program. How can I ensure that the text is typed only one time? Note that return False and l.stop() do not work as intended because they cause the script to complete and exit. One press of the hot keys should result in one instance of the text getting typed. The script should continue running until exited.

from pynput import keyboard
from pynput.keyboard import Controller, Listener
c = Controller()

def press_callback(key):
    if key.char == 'k':
        c.type("Kind regards")

l = Listener(on_press=press_callback)

l.start()
l.join()


from Pynput listener for autocomplete / hotkey typing

No comments:

Post a Comment