It is required to organize keyboard processing via the console. And you need to assign an action for pressing the button and for release. For example, pygame solves this problem, and it looks like this:

for events in pygame.event.get(): if events.type == pygame.KEYDOWN: #проверяем конкретную клавишу... if events.type == pygame.KEYUP: #проверяем конкретную клавишу... 

But in pygame, capture occurs in the created window, not in the console.

1 answer 1

The pynput module allows you to read and press the keys of the keyboard and mouse, and also to control them (ie, press, move, etc.):

 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() 

Displaying the result on the screen:

 special key Key.up pressed Key.up released special key Key.down pressed Key.down released special key Key.right pressed Key.right released special key Key.left pressed Key.left released alphanumeric key a pressed 'a' released special key Key.shift pressed Key.shift released special key Key.shift pressed alphanumeric key d pressed 'd' released Key.shift released special key Key.alt_l pressed special key Key.shift pressed alphanumeric key a pressed 'a' released Key.shift released Key.alt_l released special key Key.space pressed Key.space released special key Key.enter pressed Key.enter released special key Key.alt_l pressed Key.alt_l released 
  • Does it work on a clean console? - 0andriy
  • @ 0andriy, what do you mean by "clean console"? - MaxU
  • Car without Hove. Judging by the code there is no such support. - 0andriy
  • @ 0andriy, I don’t know, I didn’t check without X ... - MaxU
  • Right now, the right handler should subscribe to events from /dev/input/eventX and monitor their status. - 0andriy