It turns out there is no getch module on Linux, I found an alternative to pynput . I read the documentation, roughly understood how it works, tested it, but the question is: how do I connect with the listener , and later use the key test in a separate function that is in a loop. In a nutshell, check clicks in a while True:

 from pynput import keyboard def on_press(key): try: print('alphanumeric key {0} pressed'.format( key.char)) except AttributeError: print('special key {0} pressed'.format( key)) def on_release(key): print('{0} released'.format( key)) if key == keyboard.Key.esc: # Stop listener return False # Collect events until released with keyboard.Listener( on_press=on_press, on_release=on_release) as listener: listener.join() 
  • I understand the stupidity of my code, but what to do? - Ker1an
  • one
    And what is not satisfied with the documentation on this module? pynput.readthedocs.io/en/latest/keyboard.html - strawdog
  • @strawdog, yes I seem to be reading, but I can't realize it - Ker1an

1 answer 1

I suspect (strongly) that in fact your question should sound like this:

How can I write a program, so that she would listen to the keyboard all the time, but for now nothing is being pressed, was the background work?

The answer to this question is standard: you need to create a separate thread (thread) in which the code you gave in the example will be executed, and something else will be done in the main program at this time.

  • You all correctly suspected, that was exactly what I needed - Ker1an