2009-05-26 20 views
16

Come si rileva se il sistema è inattivo su Windows utilizzando Python (vale a dire senza attività della tastiera o del mouse). Questo è già stato chiesto before, ma non sembra essere un GetLastInputInfo nel modulo pywin32.rilevamento del tempo di inattività utilizzando python

+5

Da quando è stato chiesto prima, perché stai chiedendo di nuovo? Cosa pensi sia cambiato che darà una risposta diversa? –

+1

Forse c'è qualcuno in questo momento che può rispondere, ma la vecchia domanda è sepolta nell'età e nell'oscurità. Come puoi "urtare" la vecchia domanda di qualcun altro? –

+0

quando è stato chiesto in precedenza, la risposta era sul rilevamento dei clic del mouse, che non era affatto vicino a una risposta a quella domanda! – Badri

risposta

2

In realtà, è possibile accedere GetLastInputInfo attraverso il cytpes libreria:

import ctypes 
GetLastInputInfo = ctypes.windll.User32.GetLastInputInfo # callable function pointer 

Questo potrebbe non essere ciò che si vuole, però, in quanto non fornisce informazioni di standby in tutto il sistema, ma solo sulla sessione che ha chiamato la funzione. See MSDN docs.

In alternativa, è possibile check if the system is locked o se è stato avviato lo screen-saver.

18
from ctypes import Structure, windll, c_uint, sizeof, byref 

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

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

chiamata get_idle_duration() per ottenere il tempo di inattività in secondi.

+0

funziona come un fascino – Claudiu

+0

Un'altra libreria può impostare 'argtypes',' restype', o 'errcheck' su' windll.user32.GetLastInputInfo' in modo incompatibile, quindi dovresti usare la tua istanza 'WinDLL', ad es. 'user32 = WinDLL ('user32', use_last_error = True)'. – eryksun

6

Sembra come GetLastInputInfo è ora disponibile in pywin32:

win32api.GetLastInputInfo() 

fa il trucco e restituisce il timer tick dall'azione di ingresso ultimo utente.

Qui con un programma di esempio

import time 
import win32api 
for i in range(10): 
    print(win32api.GetLastInputInfo()) 
    time.sleep(1) 

Se si preme un tasto/sposta il mouse mentre lo script dorme, i cambiamenti numero stampato.

1

@ La risposta di FogleBird è piuttosto interessante e funzionante, ma di fretta non ero sicuro di come funzioni, quindi un piccolo esempio di prova qui. Un thread sta iniziando, cercando l'ultimo tempo di inattività ogni 10 secondi. Se viene effettuato un movimento all'interno di questa finestra temporale, verrà stampato.

from ctypes import Structure, windll, c_uint, sizeof, byref 
import threading 

//Print out every n seconds the idle time, when moving mouse, this should be < 10 
def printit(): 
    threading.Timer(10.0, printit).start() 
    print get_idle_duration() 



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

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

printit() 
3
import win32api 
def getIdleTime(): 
    return (win32api.GetTickCount() - win32api.GetLastInputInfo())/1000.0 
Problemi correlati