Idle detection

Windows idle-time measurement based on system tick counts for the most recent user input.

Windows API binding


source

LASTINPUTINFO

def LASTINPUTINFO(
    *args, **kwargs
):

ctypes representation of the Windows LASTINPUTINFO structure.

Exported source
class LASTINPUTINFO(ctypes.Structure):
    "ctypes representation of the Windows `LASTINPUTINFO` structure."

    _fields_ = [
        ("cbSize", wintypes.UINT),   # Size of this structure in bytes
        ("dwTime", wintypes.DWORD),  # Tick count when the last input occurred
    ]

Idle-time queries


source

get_last_input_tick

def get_last_input_tick():

Call GetLastInputInfo and return its last-input tick count in milliseconds.


source

get_idle_seconds

def get_idle_seconds():

Return the elapsed seconds between system uptime and the last input tick.

  • GetLastInputInfo returns a success flag, but the value assigned to ok is not checked. If the API call fails, get_last_input_tick() may return an unchanged or initial info.dwTime value.

  • GetTickCount64() returns a 64-bit tick count, while LASTINPUTINFO.dwTime is a 32-bit DWORD. The last-input count wraps after approximately 49.7 days of uptime, so the direct subtraction may eventually produce an incorrect idle duration.

  • The module-level info structure is mutated on every API call. Confirm whether concurrent calls are possible and whether the shared buffer needs synchronization or per-call allocation.