# 定义保存截图的文件夹路径为当前脚本所在目录下的shots文件夹 $folderPath = Join-Path -Path $PSScriptRoot -ChildPath "shots" if (-not (Test-Path -Path $folderPath)) { New-Item -ItemType Directory -Path $folderPath } # 导入用户32.dll,用于检测鼠标键盘活动 Add-Type @" using System; using System.Runtime.InteropServices; public class UserActivity { [DllImport("user32.dll")] public static extern bool GetLastInputInfo(ref LASTINPUTINFO plii); [StructLayout(LayoutKind.Sequential)] public struct LASTINPUTINFO { public uint cbSize; public uint dwTime; } } "@ # 检测用户是否活跃 function IsUserActive { $lastInput = New-Object UserActivity+LASTINPUTINFO $lastInput.cbSize = [System.Runtime.InteropServices.Marshal]::SizeOf($lastInput) if ([UserActivity]::GetLastInputInfo([ref]$lastInput)) { $idleTime = [Math]::Floor([TimeSpan]::FromMilliseconds([Environment]::TickCount - $lastInput.dwTime).TotalSeconds) # 如果空闲时间小于5秒,认为用户是活跃的 return ($idleTime -lt 1) } return $false } $durationTxt = Join-Path -Path $folderPath -ChildPath "duration.txt" $fileMatches = if (Test-Path -Path $durationTxt) { (Get-Content -Path $durationTxt -Tail 3 | Select-String -Pattern "^file '(.*\.png)'$").Matches } else { @() } $script:lastScreenshotFile = if ($fileMatches.Count -gt 0) { $fileMatches[0].Groups[1].Value } else { "" } # Write-Host "lastScreenshotFile: $lastScreenshotFile" $script:isFirstRun = $true $script:lastScreenshotTime = if ($script:lastScreenshotFile -ne "") { [DateTime]::ParseExact($script:lastScreenshotFile.Substring(0, 18), "yyyyMMdd_HHmmssfff", $null) } else { [DateTime]::MinValue } # 截屏函数 function TakeScreenshot { $now = Get-Date $timestamp = $now.ToString("yyyyMMdd_HHmmssfff") $filePath = Join-Path -Path $folderPath -ChildPath "$timestamp.png" Add-Type -AssemblyName System.Windows.Forms Add-Type -AssemblyName System.Drawing $bitmap = New-Object System.Drawing.Bitmap([System.Windows.Forms.Screen]::PrimaryScreen.Bounds.Width, [System.Windows.Forms.Screen]::PrimaryScreen.Bounds.Height) $graphics = [System.Drawing.Graphics]::FromImage($bitmap) # Capture the screen $graphics.CopyFromScreen(0, 0, 0, 0, $bitmap.Size) # Draw the cursor $cursorPosition = [System.Windows.Forms.Cursor]::Position $cursorRectangle = New-Object System.Drawing.Rectangle($cursorPosition, [System.Drawing.Size]::new(32, 32)) # Adjust size if needed # [System.Windows.Forms.Cursor]::Current.Draw($graphics, $cursorRectangle) [System.Windows.Forms.Cursors]::Arrow.Draw($graphics, $cursorRectangle) $bitmap.Save($filePath, [System.Drawing.Imaging.ImageFormat]::Png) # Add filename to duration.txt if ($script:lastScreenshotFile -ne "") { $duration = ([TimeSpan]::FromTicks(($now - $script:lastScreenshotTime).Ticks)).TotalSeconds $durationStr = "duration {0:F3}" -f $duration if ($script:isFirstRun) { $lastDuration = Get-Content -Path $durationTxt -Tail 1 | Select-String -Pattern "^duration [\d\.]+$" if ($lastDuration) { # remove last line $lines = Get-Content -Path $durationTxt $lines = $lines[0..($lines.Length - 2)] $lines += $durationStr $lines | Set-Content -Path $durationTxt } else { Add-Content -Path $durationTxt -Value $durationStr } $script:isFirstRun = $false } else { Add-Content -Path $durationTxt -Value $durationStr } } Add-Content -Path $durationTxt -Value "file '$timestamp.png'" Write-Host "Screenshot saved: $timestamp.png" [console]::beep(1000, 500) # 播放提示音 $script:lastScreenshotTime = $now $script:lastScreenshotFile = "$timestamp.png" } # Delay 3 seconds to start Start-Sleep -Seconds 3 # 主循环 while ($true) { try { if (IsUserActive) { TakeScreenshot } Start-Sleep -Seconds 1 } catch { Write-Host "An error occurred: $_" Start-Sleep -Seconds 5 # 出错时等待5秒再继续 } }