'How can I make an AFK sensor in Python?

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


Solution 1:[1]

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)

Solution 2:[2]

The package "riverpod" is for dart programs and so has all the Flutter-related classes stripped out.

Try using flutter_riverpod instead, I think that should work.

See the different riverpod packages here.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Peter Mortensen
Solution 2 LBound