I am building a screen time recorder, but I need an AFK sensor for that. I am using the pynput module for it. I was unable to break the loop in the off() function.
class key_presed(Exception): pass
def on(key):
    return k=0
    # print("o")
def off(key):
    global sec,min_,hr
    while True:
        try:
            sec+=1
            if sec==60:
                sec=0
                min_+=1
            if min_==60:
                min_=0
                hr+=1
            print(f"{hr}:{min_}:{sec}")
            with Listener(on_press=on) as l2:
                time.sleep(1)
                l2.stop()
        except Exception as e:
            print(e)
            main()
def main():
    global l
    with Listener(on_release=off) as l:
        l.join()
sec=0
min_=0
hr=0
main()
 
     
    