I'm coding a hand gesture controller and I want it press a specific button if the length variables are lower than a certain number. The problem is that this runs every millisecond and the if statement spams each line of code, running it multiple times at an instant.
if pointerThumbLength < 15:
cv2.circle(img, (centerPointerX, centerPointerY), 9, (0, 255, 0), cv2.FILLED)
keyboard.press(Key.media_play_pause)
keyboard.release(Key.media_play_pause)
if pointerMidLength < 15:
cv2.circle(img, (centerMidX, centerMidY), 9, (0, 255, 0), cv2.FILLED)
keyboard.press(Key.media_next)
keyboard.release(Key.media_next)
I want to add a cooldown after each key is pressed once at a time before it could detect it again. I tried running time.sleep() but it adds noticeable lag between the time it registers the length being lower and the time it presses the button. Any suggestions on how I could proceed? Thanks!