0

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!

  • Does this answer your question? [Understanding time.perf\_counter() and time.process\_time()](https://stackoverflow.com/questions/25785243/understanding-time-perf-counter-and-time-process-time) – GSazheniuk Jul 22 '21 at 20:18
  • you could use some global variables to set `next_execution = current_time + 2seconds` and in every execution check `if next_execution > current_time` and skip code. – furas Jul 22 '21 at 22:27
  • or maybe you should reset value `pointerThumbLength` to something what would skip `if` – furas Jul 22 '21 at 22:30

0 Answers0