Home > Back-end >  python non-terminating keyboard interrupt
python non-terminating keyboard interrupt

Time:05-20

Not sure of the terminology here.

I have a timer loop

 flag=False
 for second in range(10*60) :
   do_something
   if flag :
     add_trick
     flag = False
  
     

I want to press a special key at any arbitrary point that sets flag.

I've tried adding a key listener. Here's my test code.

 from pynput import keyboard
 
 flag=False
 def on_press(key):
     global flag
     match str(key) :
         case "'s'" : 
             flag=True
     return True
     
 def run_timer():
     global flag
     flag=False
     while True: # for second in range(10*60) :
       #sleep(1)
       print('a')
       if flag :
          print('FLAG')
          flag = False
 
 
 
 if __name__=="__main__" :
     listener = keyboard.Listener(on_press=on_press)
     listener.start() 
     #listener.join()
     run_timer()
   

It doesn't work.

Without the listener.join() the listener doesn't engage.

With the listener.join() the listener blocks and the loop doesn't run.

CodePudding user response:

The key parameter in the callback is not a string, it's a pynput.keyboard.Key object. So instead of comparing it to "s", you need to compare key.char to "s".

Here's a cleaned up version of your test code that will set the flag 3 times and then cleaning up the listener thread and exiting:

import time                                                                                         
from pynput import keyboard                                                                         
                                                                                                    
flag = False                                                                                        
def on_press(key):                                                                                  
    global flag                                                                                     
    if key.char == "s":                                                                             
        flag = True                                                                                 
    return True                                                                                     
                                                                                                    
def run_timer():                                                                                    
    global flag                                                                                     
    flag = False                                                                                    
    flag_count = 0                                                                                  
    while flag_count < 3:                                                                           
        time.sleep(0.1)                                                                                
        if flag :                                                                                   
            print('FLAG')                                                                           
            flag = False                                                                            
            flag_count  = 1                                                                         
                                                                                                    
if __name__=="__main__" :                                                                           
    listener = keyboard.Listener(on_press=on_press)                                                 
    listener.start()                                                                                
    run_timer()                                                                                     
    listener.stop()                                                                                 
    listener.join()                                                                                 
    print("listener cleaned up")
  • Related