'python - Pynput loop printing text

Hi I am learning about pynput and I wanted to make a code that I will be able to use it on wordle. My intention is that if I press backspace, it will type in "nymph, quick, gybes, waltz, fjord." However, when I press backspace once, it types these set of word twice instead of only once. Or it types the word wrongly like "nynymph" instead of "nymph"

My guess is that it detects backspace multiple times while it's pressed. How do you make sure the function is only executed once?

If I am wrong, what is the cause of this?

import time
from pynput.keyboard import Key,Controller,Listener

keyboard = Controller()

def show(key):
    if key == Key.backspace:
        keyboard.type("nymph")
        keyboard.press(Key.enter)
        keyboard.release(Key.enter)
        keyboard.type("quick")
        keyboard.press(Key.enter)
        keyboard.release(Key.enter)
        keyboard.type("gybes")
        keyboard.press(Key.enter)
        keyboard.release(Key.enter)
        keyboard.type("waltz")
        keyboard.press(Key.enter)
        keyboard.release(Key.enter)
        keyboard.type("fjord")
        keyboard.press(Key.enter)
        keyboard.release(Key.enter)
        return False

with Listener(on_press = show) as listener:
    listener.join()


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source