sticky key when using "keyboard.on_press_key()"

Viewed 31

Function is triggered after I release the key for some more time.

If you hold down 'E' for 5 seconds, the 'E' key will be pressed ~10 times during this time, then the function will be played all ~10 times, even if the key has already been released

How to make the function work only if the E key is held down, and after releasing the function stops?

import keyboard # https://github.com/boppreh/keyboard#keyboard.on_press_key
import mouse # https://github.com/boppreh/mouse#mouse.is_pressed
from time import sleep

def ButtonTest(KBEvent):
    print("Key W: ", keyboard.is_pressed('w'))
    print("Right Mouse: ", mouse.is_pressed(button='right'))
    keyboard.press('ctrl')
    sleep(1)
    keyboard.release('ctrl')

keyboard.on_press_key('e', ButtonTest)
keyboard.wait()
1 Answers

You probably don't want to use time.sleep() inside your function because it's thread-blocking. If what you want is to have time intervals between callback function calls, consider using keyboard.wait() instead.

Related