获取给定时间范围内Windows计算机的活动时间

pexxcrt2  于 2023-04-07  发布在  Windows
关注(0)|答案(1)|浏览(147)

我想看看我是否可以得到在给定的时间范围内我在系统上花费的总时间。示例:从上午9点到晚上9点,我应该确定我的机器处于锁定状态的时间、我的机器处于睡眠模式的时间以及我的机器处于活动状态的时间。
我可以从最后一次 Boot 时获得它,但是如果我几天没有关闭系统怎么办?我可以获得我在机器上花费的活动时间吗?
还有,有没有一种方法可以让我得到我花在应用程序上的时间?示例:我已经安装了Teams、Browser-Chrome和IDE。我是否可以获得在给定时间范围内花费在应用程序上的时间?

wwwo4jvm

wwwo4jvm1#

给予这个。用Autoit(www.autoitscript.com)编写的。当然,你可以通过检查活动窗口标题来扩展它,获取相应的进程,然后计算在该特定应用程序上花费的时间。这只是一个开始。

; workstation idle logger for windows
; Functions : IsWorkStationLocked() and IsScreenSaverRunning()
;

#include <Date.au3>

DllOpen("user32.dll")

$actiondate =  _NowCalc()
$timehaspastmin = 0
$timehaspasthour =0
$tmpString1=0
$tmpString2=0
$prediction="was working for"

While 1

        $actiontrigger = False

    $file = FileOpen("lockstatus.txt", 1)
    If $file = -1 Then
        MsgBox(0, "Error", "Unable to open file.")
        Exit
    EndIf

    $tmpString1=IsWorkStationLocked()
    $tmpString2=IsScreenSaverRunning()

    Sleep(10000)

    if $tmpString1 <> IsWorkStationLocked() Then
        $actiontrigger = True

    EndIf

    if  $tmpString2 <> IsScreenSaverRunning() Then
        $actiontrigger = True

    EndIf

    if $actiontrigger = True Then
        $timehaspastsec = _Datediff ( 's' , $actiondate , _NowCalc())
        $timehaspastmin = Round($timehaspastsec / 60,1)
        $timehaspasthour = Round($timehaspastmin /60,1)

        select
            case $timehaspasthour >= 1
                $timetype="hour(s)"
                $timehaspast=$timehaspasthour
            case $timehaspastmin >= 1
                $timetype="minute(s)"
                $timehaspast=$timehaspastmin
            case Else
                $timetype="second(s)"
                $timehaspast=$timehaspastsec
        EndSelect

        select
        case IsScreenSaverRunning() = False and IsWorkStationLocked() = False
            $prediction = "was away for"
        case IsScreenSaverRunning() = True and IsWorkStationLocked() = False
            $prediction = "was working for"
        case IsScreenSaverRunning() = True and IsWorkStationLocked() = True
            $prediction = "(really idle now) was watching my screen for "
        case IsScreenSaverRunning() = False and IsWorkStationLocked() = True
            $prediction = "was working for"
        EndSelect

        $outputstring =  _NowCalc() & " - " & "scrsaver=" & IsScreenSaverRunning() & ",Locked=" & IsWorkStationLocked() & ", " & $prediction & " " & $timehaspast & " " & $timetype & @CRLF
        FileWrite($file , $outputstring )
        ConsoleWrite ( $outputstring)
        $actiondate =  _NowCalc()
    EndIf

    FileClose($file)
WEnd

Func IsWorkStationLocked()
    Local $Desktop, $Return

    $Return = DllCall("user32.dll", "long", "OpenDesktopA", "str", "Default", "dword", 0, "ubyte", 0, "dword", 0x100)
    $Desktop = $Return[0]

    If $Desktop = 0 Then
        Return True
    EndIf

    $Return = DllCall("user32.dll", "long", "SwitchDesktop", "dword", $Desktop)
    DllCall("user32.dll", "long", "CloseDesktop", "dword", $Desktop)

    If $Return[0] = 0 Then
        Return True
    Else
        Return False
    EndIf
EndFunc

Func IsScreenSaverRunning()
    Local $Desktop, $Return, $Result

    $Result = DllStructCreate("uint")
    $Return = DllCall("user32.dll", "ubyte", "SystemParametersInfoA", "uint", 0x72, "uint", 0, "ptr", DllStructGetPtr($Result), "uint", 0)
    If $Return[0] = 0 Then Return SetError( 0, False)

    Return DllStructGetData($Result, 1) <> 0
EndFunc

相关问题