-2

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()
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • What is *"AFK"* in *"AFK sensor"*? *[away from keyboard](https://en.wiktionary.org/wiki/AFK#Prepositional_phrase)*? – Peter Mortensen May 24 '22 at 11:21
  • Related: *[How can I break out of multiple loops?](https://stackoverflow.com/questions/189645/)* and *[Breaking out of nested loops](https://stackoverflow.com/questions/653509/)*. – Peter Mortensen May 24 '22 at 11:28

1 Answers1

0

OK, I got an answer. I made functions to change the variable in the loop and break it by if statements:

from pynput.keyboard import *
import time

def changer(key):
    global c

    if key==False:
        return c
    else:
        c+=1
        return c

def timer():
    global c
    c=0
    sec=0
    min=0
    hr=0
    while True:
        sec+=1
        c=changer(key=False)
        if sec==60:
            sec=0
            min+=1
        if min==60:
            min=0
            hr+=1
        if c<1:
            pass
        else :
            loop(key=None)
            break
        def connector(key):
            changer(key=True)
        print(f"{hr}:{min}:{sec}")
        with Listener(on_press=connector) as l:
            time.sleep(1)
            l.stop()

def afk_f(var):
    global afk

    if var==True:
        afk+=1
        return afk

    else:
        afk=0
        return afk

def loop(key):
    global afk
    afk=0

    while afk<10:
        def reset(key):
            return afk_f(var=False),False
        with Listener(on_press=reset) as l:
            time.sleep(1)
            l.stop()
        afk=afk_f(var=True)
        print(afk)
    timer()

loop(key=None)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
lecrowpus
  • 1
  • 1
  • 3
    Next step: Try to refactor the code so that you don't need `global` anymore. – Matthias May 16 '22 at 07:39
  • 1
    What ***exactly*** did you change? What functions? What variable? What loop? What *if* statements? Please respond by [editing (changing) your answer](https://stackoverflow.com/posts/72255644/edit), not here in comments (***without*** "Edit:", "Update:", or similar - the answer should appear as if it was written today). Thanks in advance. – Peter Mortensen May 24 '22 at 10:38