简体   繁体   中英

Python get idle duration

im making a script that get idle duration getting the last mouse/kayboard input time and in windows it worked but i canot make it run on linux

class LASTINPUTINFO(Structure):
    _fields_ = [
        ('cbSize', c_uint),
        ('dwTime', c_uint),
    ]

def get_idle_duration():
    # get idle time
    lastInputInfo = LASTINPUTINFO()
    lastInputInfo.cbSize = sizeof(lastInputInfo)
    windll.user32.GetLastInputInfo(byref(lastInputInfo))
    millis = windll.kernel32.GetTickCount() - lastInputInfo.dwTime
    return millis / 1000.0

print(get_idle_duration())

some one can help me to make this code run in linux and windows?

You can refer idle.py by Gajim

Just call the getIdleSec() to get the idle time in seconds.

If you execute idle.py , it will wait for 2.1 seconds and then print the idle time. Later it frees the held data after which if you call the function, it will return zero.

The problem with the code given in the question is that it uses windll which is specific to Windows.

If you want to see the script in action in a more detailed manner, you can change main as follows:

if __name__ == '__main__':
    import time
    for i in range(0,10):
        print(getIdleSec())
    close()

This works on Ubuntu and should work on all Debian based operating systems.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM