Partage
  • Partager sur Facebook
  • Partager sur Twitter

Keys kept in memory during keyboard listener

    10 juin 2022 à 10:21:35

    Hello,

    I'm using  "keyboard" from "pynput" to detect the keys pressed  on keyboard.
    Here's my code :

    from pynput import keyboard
    
    kb = keyboard.Controller()
    
    def listener():
        number = 0  
        with keyboard.Events() as events:
            for event in events:
                if hasattr(event.key, "vk") and 96 == event.key.vk:    # 0
                    number = 0
                    break
                if hasattr(event.key, "vk") and 97 == event.key.vk:    # 1
                    number = 1
                    break
            #kb.press(keyboard.Key.esc)
            #kb.release(keyboard.Key.esc)
        return number
    
    print("Press 0 or 1\n")
    number = listener()
    if number == 0:
        print("number 0 detected")
        input("-> ")
    elif number == 1:
        print("number 1 detected")
        input("-> ")



    The user press 0 or 1 to escape the listener. My problem is when the user press other keys on keyboard, letters for exemple are kept in memory, the input is fill with the letters pressed :

    number 1 detected
    -> test1


    To avoid this a simulation of pressing ESC allow to clear the letters in memory (the lines in comment) BUT if the user press ENTER before 0 or 1 the input is instantly validated with the keys pressed before enter.
    The simulation of ESC doesn't resolve this

    Is it possible to delete everything in memory before the input command ?

    Thank you

    -
    Edité par HugoTanneau 10 juin 2022 à 10:22:22

    • Partager sur Facebook
    • Partager sur Twitter
      31 août 2024 à 19:51:41 - Message modéré pour le motif suivant : Toute forme de publicité est interdite


      Keys kept in memory during keyboard listener

      × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
      • Editeur
      • Markdown