Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

178
Views
Suppress user input keypress while executing autogenerated keypress Python

My problem here is I want to have any keystroke the user types execute a automatic keystroke. However, I would like only the user generated keystroke to be suppressed. I am trying to do this with pynput like this:

def on_press(key):
    if key == keyboard.Key.esc:
        return False

def on_release(key):
    if key == keyboard.Key.esc:
        return False
    elif len(words) > i:
        c.press(words[i])
        i += 1
    else:
        c.press(keyboard.Key.space)
        return False

with keyboard.Listener(on_press=on_press, on_release=on_release, suppress=True) as listener:
    listener.join()

I think that the problem is that the keypress call inside of the on_press() function also calls to the listener function and this creates infinite recursion. If I keep the suppressor on nothing is typed but the program keeps infinitely recursing. If I turn the suppressor off both the user keypress is executed and the autogenerated key press is executed recursively.

But if I keypress outside of the on_press() function like below it only executes if I exit the listener.

with keyboard.Listener(on_press=on_press, on_release=on_release, suppress=True) as listener:
    listener.join()
    if key == keyboard.Key.esc:
        return False
    elif len(words) > i:
        c.press(words[i])
        i += 1
    else:
        c.press(keyboard.Key.space)
        return False

Does anyone have a workaround to this?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!