Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

179
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda