1 +$raw = [Environment]::GetCommandLineArgs()
3 +$scriptName = [IO.Path]::GetFileName($PSCommandPath)
4 +for ($i = 0; $i -lt $raw.Count; $i++) {
6 + if ([IO.Path]::GetFileName([string]$raw[$i]) -eq $scriptName) { $scriptIndex = $i }
10 +if ($scriptIndex -ge 0) {
11 + for ($i = $scriptIndex + 1; $i -lt $raw.Count; $i++) { $argv += [string] $raw[$i] }
15 +if ($argv.Count -gt 0) {
16 + $Command = [string] $argv[0]
17 + if ($argv.Count -gt 1) { $Rest = @($argv[1..($argv.Count - 1)]) }
20 +$ErrorActionPreference = 'Stop'
21 +Add-Type -AssemblyName System.Drawing,System.Windows.Forms
25 +using System.Runtime.InteropServices;
27 +public static class Win32DriveNative {
28 + public delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
30 + [StructLayout(LayoutKind.Sequential)] public struct RECT { public int Left; public int Top; public int Right; public int Bottom; }
31 + [StructLayout(LayoutKind.Sequential)] public struct POINT { public int X; public int Y; }
32 + [StructLayout(LayoutKind.Sequential)] public struct LASTINPUTINFO { public uint cbSize; public uint dwTime; }
34 + [DllImport("user32.dll")] public static extern bool EnumWindows(EnumWindowsProc cb, IntPtr lParam);
35 + [DllImport("user32.dll")] public static extern bool IsWindowVisible(IntPtr hWnd);
36 + [DllImport("user32.dll")] public static extern bool IsWindow(IntPtr hWnd);
37 + [DllImport("user32.dll", CharSet=CharSet.Unicode)] public static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
38 + [DllImport("user32.dll")] public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint pid);
39 + [DllImport("user32.dll")] public static extern bool GetWindowRect(IntPtr hWnd, out RECT rect);
40 + [DllImport("dwmapi.dll")] public static extern int DwmGetWindowAttribute(IntPtr hwnd, int attr, out RECT rect, int size);
41 + [DllImport("user32.dll")] public static extern bool PrintWindow(IntPtr hwnd, IntPtr hdcBlt, uint flags);
42 + [DllImport("user32.dll")] public static extern bool SetForegroundWindow(IntPtr hWnd);
43 + [DllImport("user32.dll")] public static extern bool MoveWindow(IntPtr hWnd, int x, int y, int w, int h, bool repaint);
44 + [DllImport("user32.dll")] public static extern bool ShowWindow(IntPtr hWnd, int cmd);
45 + [DllImport("user32.dll")] public static extern bool PostMessage(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);
46 + [DllImport("user32.dll")] public static extern bool SetCursorPos(int x, int y);
47 + [DllImport("user32.dll")] public static extern void mouse_event(uint flags, uint dx, uint dy, uint data, UIntPtr extraInfo);
48 + [DllImport("user32.dll")] public static extern bool GetCursorPos(out POINT point);
49 + [DllImport("user32.dll")] public static extern bool GetLastInputInfo(ref LASTINPUTINFO info);
50 + [DllImport("user32.dll", CharSet=CharSet.Unicode, SetLastError=true)] public static extern IntPtr SendMessageTimeout(IntPtr hWnd, uint msg, UIntPtr wParam, string lParam, uint flags, uint timeout, out UIntPtr result);
51 + [DllImport("kernel32.dll")] public static extern uint GetTickCount();
55 +function Write-Json($Value) {
56 + $Value | ConvertTo-Json -Depth 24 -Compress
59 +function Parse-Options([string[]] $ArgList) {
62 + $valueOptions = @('Hwnd','Process','TitleContains','X','Y','Width','Height','Out','Text','Keys','Action','Button','Count','DurationMs','IdleMs','TimeoutMs','Set','Depth','Mode','Scope')
63 + for ($i = 0; $i -lt $ArgList.Count; $i++) {
65 + if ($arg.StartsWith('-')) {
66 + $key = $arg.TrimStart('-')
67 + if ($valueOptions -notcontains $key) { throw "Unknown option: -$key" }
68 + if ($i + 1 -ge $ArgList.Count) { throw "Missing value for -$key" }
70 + $opts[$key] = $ArgList[$i]
75 + if ($pos.Count -gt 0) { throw ('Unexpected positional args: ' + ($pos -join ' ')) }
79 +function Int-Opt($Opts, [string] $Name, $Default = $null) {
80 + if (-not $Opts.ContainsKey($Name)) { return $Default }
84 +function Long-Opt($Opts, [string] $Name, $Default = $null) {
85 + if (-not $Opts.ContainsKey($Name)) { return $Default }
86 + [int64] $Opts[$Name]
89 +function Str-Opt($Opts, [string] $Name, $Default = $null) {
90 + if (-not $Opts.ContainsKey($Name)) { return $Default }
91 + [string] $Opts[$Name]
94 +function Get-Windows {
95 + $script:win32DriveWindows = @()
96 + [Win32DriveNative]::EnumWindows({ param($h, $l)
97 + if ([Win32DriveNative]::IsWindowVisible($h)) {
98 + $sb = [Text.StringBuilder]::new(1024)
99 + [void] [Win32DriveNative]::GetWindowText($h, $sb, $sb.Capacity)
100 + $title = $sb.ToString()
101 + if ($title.Trim().Length -gt 0) {
102 + [uint32] $procId = 0
103 + [void] [Win32DriveNative]::GetWindowThreadProcessId($h, [ref] $procId)
104 + $proc = Get-Process -Id $procId -ErrorAction SilentlyContinue
105 + $rect = Get-Rect $h
106 + $script:win32DriveWindows += [pscustomobject] [ordered] @{
107 + hwnd = $h.ToInt64()
108 + pid = [int64] $procId
109 + process = if ($proc) { $proc.ProcessName } else { $null }
113 + width = $rect.Right - $rect.Left
114 + height = $rect.Bottom - $rect.Top
119 + }, [IntPtr]::Zero) | Out-Null
120 + $script:win32DriveWindows
123 +function Get-Rect([IntPtr] $Hwnd) {
124 + $rect = New-Object Win32DriveNative+RECT
125 + $size = [Runtime.InteropServices.Marshal]::SizeOf([type] [Win32DriveNative+RECT])
126 + $hr = [Win32DriveNative]::DwmGetWindowAttribute($Hwnd, 9, [ref] $rect, $size)
127 + if ($hr -ne 0) { [void] [Win32DriveNative]::GetWindowRect($Hwnd, [ref] $rect) }
131 +function Resolve-Window($Opts) {
132 + if ($Opts.ContainsKey('Hwnd')) {
133 + $hwnd = [IntPtr] (Long-Opt $Opts 'Hwnd')
134 + if (-not [Win32DriveNative]::IsWindow($hwnd)) { throw 'Invalid window handle' }
137 + $wins = @(Get-Windows)
138 + $proc = Str-Opt $Opts 'Process'
139 + $title = Str-Opt $Opts 'TitleContains'
140 + if ($proc) { $wins = @($wins | Where-Object { $_.process -eq $proc }) }
141 + if ($title) { $wins = @($wins | Where-Object { $_.title.IndexOf($title, [StringComparison]::OrdinalIgnoreCase) -ge 0 }) }
142 + if ($wins.Count -eq 0) { throw 'No matching window' }
143 + if ($wins.Count -gt 1) { throw ('Ambiguous window: ' + (($wins | Select-Object -First 8 hwnd,process,title) | ConvertTo-Json -Compress)) }
144 + [IntPtr] $wins[0].hwnd
147 +function Get-IdleMs {
148 + $info = New-Object Win32DriveNative+LASTINPUTINFO
149 + $info.cbSize = [Runtime.InteropServices.Marshal]::SizeOf($info)
150 + [void] [Win32DriveNative]::GetLastInputInfo([ref] $info)
151 + [int64] ([Win32DriveNative]::GetTickCount() - $info.dwTime)
154 +function Wait-UserIdle([int] $IdleMs, [int] $TimeoutMs) {
156 + while ((Get-IdleMs) -lt $IdleMs) {
157 + if (((Get-Date) - $start).TotalMilliseconds -gt $TimeoutMs) { throw "User not idle for $IdleMs ms" }
158 + Start-Sleep -Milliseconds 100
162 +function Draw-Indicator($X = $null, $Y = $null, [int] $DurationMs = 180) {
164 + foreach ($screen in [Windows.Forms.Screen]::AllScreens) {
165 + $b = $screen.Bounds
168 + $form = New-Object Windows.Forms.Form
169 + $form.FormBorderStyle = 'None'
170 + $form.StartPosition = 'Manual'
172 + $form.TopMost = $true
173 + $form.BackColor = [Drawing.Color]::Magenta
174 + $form.TransparencyKey = [Drawing.Color]::Magenta
175 + $form.ShowInTaskbar = $false
176 + $form.Opacity = 0.0
178 + param($sender, $event)
179 + $event.Graphics.SmoothingMode = [Drawing.Drawing2D.SmoothingMode]::AntiAlias
180 + $pen = New-Object Drawing.Pen ([Drawing.Color]::Red), 16
182 + $event.Graphics.DrawRectangle($pen, 8, 8, $sender.Width - 17, $sender.Height - 17)
183 + if ($targetX -ne $null -and $targetY -ne $null -and $b.Contains([Drawing.Point]::new([int]$targetX, [int]$targetY))) {
184 + $localX = [int]$targetX - $b.Left
185 + $localY = [int]$targetY - $b.Top
186 + $event.Graphics.DrawEllipse($pen, $localX - 24, $localY - 24, 48, 48)
195 + foreach ($opacity in 0.2,0.45,0.7,0.9) {
196 + foreach ($form in $forms) { $form.Opacity = $opacity; $form.Invalidate() }
197 + [Windows.Forms.Application]::DoEvents()
198 + Start-Sleep -Milliseconds 20
200 + if ($DurationMs -gt 0) { Start-Sleep -Milliseconds $DurationMs }
201 + foreach ($opacity in 0.7,0.45,0.2,0.0) {
202 + foreach ($form in $forms) { $form.Opacity = $opacity }
203 + [Windows.Forms.Application]::DoEvents()
204 + Start-Sleep -Milliseconds 20
206 + foreach ($form in $forms) { $form.Close(); $form.Dispose() }
207 + [Windows.Forms.Application]::DoEvents()
210 +function Invoke-RacingGuard($Opts, $X = $null, $Y = $null) {
211 + $idle = Int-Opt $Opts 'IdleMs' 800
212 + $timeout = Int-Opt $Opts 'TimeoutMs' 10000
213 + Draw-Indicator $X $Y 80
214 + Wait-UserIdle $idle $timeout
215 + Draw-Indicator $X $Y 80
218 +function Theme-Name($Value) {
219 + if ([int]$Value -eq 0) { 'dark' } else { 'light' }
222 +function Theme-Value([string] $Mode) {
223 + switch ($Mode.ToLowerInvariant()) {
226 + default { throw 'Mode must be dark or light' }
230 +function Get-Theme {
231 + $path = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize'
232 + $item = Get-ItemProperty -Path $path -ErrorAction SilentlyContinue
233 + $apps = if ($item -and $null -ne $item.AppsUseLightTheme) { $item.AppsUseLightTheme } else { 1 }
234 + $system = if ($item -and $null -ne $item.SystemUsesLightTheme) { $item.SystemUsesLightTheme } else { 1 }
235 + [pscustomobject] [ordered] @{ ok = $true; apps = Theme-Name $apps; system = Theme-Name $system }
238 +function Broadcast-ThemeChange {
239 + $result = [UIntPtr]::Zero
240 + foreach ($name in @('ImmersiveColorSet','WindowsThemeElement','Software\Microsoft\Windows\CurrentVersion\Themes\Personalize')) {
241 + [void] [Win32DriveNative]::SendMessageTimeout([IntPtr]0xffff, 0x001A, [UIntPtr]::Zero, $name, 2, 5000, [ref] $result)
243 + [void] [Win32DriveNative]::SendMessageTimeout([IntPtr]0xffff, 0x031A, [UIntPtr]::Zero, $null, 2, 5000, [ref] $result)
244 + Start-Process -WindowStyle Hidden -FilePath rundll32.exe -ArgumentList 'user32.dll,UpdatePerUserSystemParameters 1, True' -Wait
247 +function Set-Theme([string] $Mode, [string] $Scope) {
248 + $value = Theme-Value $Mode
249 + $scopeName = if ($Scope) { $Scope.ToLowerInvariant() } else { 'both' }
250 + if (@('apps','system','both') -notcontains $scopeName) { throw 'Scope must be apps, system, or both' }
251 + $path = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize'
252 + New-Item -Path $path -Force | Out-Null
253 + if ($scopeName -eq 'apps' -or $scopeName -eq 'both') { Set-ItemProperty -Path $path -Name AppsUseLightTheme -Type DWord -Value $value }
254 + if ($scopeName -eq 'system' -or $scopeName -eq 'both') { Set-ItemProperty -Path $path -Name SystemUsesLightTheme -Type DWord -Value $value }
255 + Broadcast-ThemeChange
256 + Get-Theme | Add-Member -PassThru -NotePropertyName scope -NotePropertyValue $scopeName
259 +function Save-Bitmap($Bitmap, [string] $Out) {
260 + if (-not $Out) { $Out = Join-Path $env:TEMP ("win32-drive-shot-" + [guid]::NewGuid().ToString('N') + '.png') }
261 + $full = [IO.Path]::GetFullPath($Out)
262 + $parent = Split-Path -Parent $full
263 + if ($parent -and -not (Test-Path $parent)) { New-Item -ItemType Directory -Force -Path $parent | Out-Null }
264 + $Bitmap.Save($full, [Drawing.Imaging.ImageFormat]::Png)
268 +function Get-PixelColor([int] $X, [int] $Y) {
269 + $bmp = [Drawing.Bitmap]::new(1, 1)
270 + $g = [Drawing.Graphics]::FromImage($bmp)
272 + $g.CopyFromScreen($X, $Y, 0, 0, [Drawing.Size]::new(1, 1))
273 + $c = $bmp.GetPixel(0, 0)
274 + [pscustomobject] [ordered] @{ ok = $true; x = $X; y = $Y; r = $c.R; g = $c.G; b = $c.B; hex = ('#{0:X2}{1:X2}{2:X2}' -f $c.R, $c.G, $c.B) }
276 + $g.Dispose(); $bmp.Dispose()
280 +function Capture-Region([int] $X, [int] $Y, [int] $Width, [int] $Height, [string] $Out) {
281 + $bmp = [Drawing.Bitmap]::new($Width, $Height)
282 + $g = [Drawing.Graphics]::FromImage($bmp)
284 + $g.CopyFromScreen($X, $Y, 0, 0, $bmp.Size)
285 + $path = Save-Bitmap $bmp $Out
286 + [pscustomobject] [ordered] @{ ok = $true; path = $path; x = $X; y = $Y; width = $Width; height = $Height; mode = 'region' }
288 + $g.Dispose(); $bmp.Dispose()
292 +function Capture-Window([IntPtr] $Hwnd, [string] $Out) {
293 + $r = Get-Rect $Hwnd
294 + $w = $r.Right - $r.Left
295 + $h = $r.Bottom - $r.Top
296 + if ($w -le 0 -or $h -le 0) { throw 'Window has empty bounds' }
297 + $bmp = [Drawing.Bitmap]::new($w, $h)
298 + $g = [Drawing.Graphics]::FromImage($bmp)
299 + $used = 'printwindow'
302 + $ok = [Win32DriveNative]::PrintWindow($Hwnd, $hdc, 2)
303 + $g.ReleaseHdc($hdc)
305 + $used = 'screen-crop'
306 + $g.CopyFromScreen($r.Left, $r.Top, 0, 0, $bmp.Size)
308 + $path = Save-Bitmap $bmp $Out
309 + [pscustomobject] [ordered] @{ ok = $true; path = $path; hwnd = $Hwnd.ToInt64(); x = $r.Left; y = $r.Top; width = $w; height = $h; mode = $used }
311 + $g.Dispose(); $bmp.Dispose()
315 +function Dump-Uia($Element, [int] $Depth, [int] $MaxDepth) {
316 + if ($null -eq $Element) { return $null }
317 + $rect = $Element.Current.BoundingRectangle
318 + $item = [ordered] @{
319 + name = $Element.Current.Name
320 + automationId = $Element.Current.AutomationId
321 + className = $Element.Current.ClassName
322 + controlType = $Element.Current.ControlType.ProgrammaticName
325 + width = [int] $rect.Width
326 + height = [int] $rect.Height
329 + if ($Depth -lt $MaxDepth) {
330 + $children = $Element.FindAll([Windows.Automation.TreeScope]::Children, [Windows.Automation.Condition]::TrueCondition)
331 + foreach ($child in $children) { $item.children += Dump-Uia $child ($Depth + 1) $MaxDepth }
333 + [pscustomobject] $item
336 +function Escape-SendKeysText([string] $Text) {
337 + $special = '+^%~()[]'
338 + $out = New-Object Text.StringBuilder
339 + foreach ($ch in $Text.ToCharArray()) {
342 + "`n" { [void] $out.Append('{ENTER}'); break }
343 + "`t" { [void] $out.Append('{TAB}'); break }
344 + '{' { [void] $out.Append('{{}'); break }
345 + '}' { [void] $out.Append('{}}'); break }
347 + if ($special.IndexOf($ch) -ge 0) { [void] $out.Append('{').Append($ch).Append('}') }
348 + else { [void] $out.Append($ch) }
355 +function Show-Help {
356 + [pscustomobject] [ordered] @{
358 + runtime = 'Windows PowerShell 5.1+'
359 + coordinates = 'screen pixels; when -Hwnd and -X/-Y are used for click, X/Y are window-relative'
360 + racingGuard = 'racing commands fade a 16px red border/ring, wait for user idle, then act'
361 + commands = [ordered] @{
362 + help = [ordered] @{ racing = $false; usage = 'help' }
363 + list = [ordered] @{ racing = $false; usage = 'list' }
364 + shot = [ordered] @{ racing = $false; usage = 'shot [-Hwnd n|-Process p -TitleContains t|-X x -Y y -Width w -Height h] [-Out file.png]' }
365 + pixel = [ordered] @{ racing = $false; usage = 'pixel -X x -Y y' }
366 + uia = [ordered] @{ racing = $false; usage = 'uia [-Hwnd n|-Process p|-TitleContains t] [-Depth n]' }
367 + cursor = [ordered] @{ racing = $false; usage = 'cursor' }
368 + idle = [ordered] @{ racing = $false; usage = 'idle' }
369 + clipboard = [ordered] @{ racing = $null; usage = 'clipboard | clipboard -Set text' }
370 + theme = [ordered] @{ racing = $true; usage = 'theme | theme -Mode dark|light [-Scope apps|system|both]' }
371 + overlay = [ordered] @{ racing = $false; usage = 'overlay [-X x -Y y] [-DurationMs ms]' }
372 + click = [ordered] @{ racing = $true; usage = 'click [-Hwnd n|-Process p|-TitleContains t] -X x -Y y [-Button left|right] [-Count n]' }
373 + type = [ordered] @{ racing = $true; usage = 'type [-Hwnd n|-Process p|-TitleContains t] -Text text' }
374 + key = [ordered] @{ racing = $true; usage = 'key [-Hwnd n|-Process p|-TitleContains t] -Keys "^s"' }
375 + window = [ordered] @{ racing = $true; usage = 'window -Action move|min|max|restore|close -Hwnd n [-X x -Y y -Width w -Height h]' }
380 +function Invoke-CommandImpl {
381 + $opts = Parse-Options $Rest
382 + switch ($Command.ToLowerInvariant()) {
383 + 'help' { return Show-Help }
384 + 'list' { return [pscustomobject] [ordered] @{ ok = $true; windows = @(Get-Windows) } }
385 + 'idle' { return [pscustomobject] [ordered] @{ ok = $true; idleMs = Get-IdleMs } }
387 + $p = New-Object Win32DriveNative+POINT
388 + [void] [Win32DriveNative]::GetCursorPos([ref] $p)
389 + return [pscustomobject] [ordered] @{ ok = $true; x = $p.X; y = $p.Y }
392 + Draw-Indicator (Int-Opt $opts 'X') (Int-Opt $opts 'Y') (Int-Opt $opts 'DurationMs' 650)
393 + return [pscustomobject] [ordered] @{ ok = $true }
396 + $x = Int-Opt $opts 'X'
397 + $y = Int-Opt $opts 'Y'
398 + if ($x -eq $null -or $y -eq $null) { throw 'pixel needs -X and -Y' }
399 + return Get-PixelColor $x $y
402 + $out = Str-Opt $opts 'Out'
403 + if ($opts.ContainsKey('Hwnd') -or $opts.ContainsKey('Process') -or $opts.ContainsKey('TitleContains')) { return Capture-Window (Resolve-Window $opts) $out }
404 + $screen = [System.Windows.Forms.SystemInformation]::VirtualScreen
405 + $x = Int-Opt $opts 'X' $screen.Left
406 + $y = Int-Opt $opts 'Y' $screen.Top
407 + $w = Int-Opt $opts 'Width' $screen.Width
408 + $h = Int-Opt $opts 'Height' $screen.Height
409 + return Capture-Region $x $y $w $h $out
412 + Add-Type -AssemblyName UIAutomationClient,UIAutomationTypes
413 + $depth = Int-Opt $opts 'Depth' 2
414 + if ($opts.ContainsKey('Hwnd') -or $opts.ContainsKey('Process') -or $opts.ContainsKey('TitleContains')) {
415 + $el = [Windows.Automation.AutomationElement]::FromHandle((Resolve-Window $opts))
417 + $el = [Windows.Automation.AutomationElement]::FocusedElement
419 + return [pscustomobject] [ordered] @{ ok = $true; tree = Dump-Uia $el 0 $depth }
422 + if ($opts.ContainsKey('Set')) {
423 + Invoke-RacingGuard $opts
424 + [Windows.Forms.Clipboard]::SetText((Str-Opt $opts 'Set'))
425 + return [pscustomobject] [ordered] @{ ok = $true; action = 'set' }
427 + return [pscustomobject] [ordered] @{ ok = $true; text = [Windows.Forms.Clipboard]::GetText() }
430 + if (-not $opts.ContainsKey('Mode')) { return Get-Theme }
431 + Invoke-RacingGuard $opts
432 + return Set-Theme (Str-Opt $opts 'Mode') (Str-Opt $opts 'Scope' 'both')
435 + $button = (Str-Opt $opts 'Button' 'left').ToLowerInvariant()
436 + $count = Int-Opt $opts 'Count' 1
437 + $x = Int-Opt $opts 'X'
438 + $y = Int-Opt $opts 'Y'
440 + if ($opts.ContainsKey('Hwnd') -or $opts.ContainsKey('Process') -or $opts.ContainsKey('TitleContains')) {
441 + $hwnd = Resolve-Window $opts
442 + $r = Get-Rect $hwnd
443 + [void] [Win32DriveNative]::SetForegroundWindow($hwnd)
444 + if ($x -eq $null) { $x = [int] (($r.Left + $r.Right) / 2) } else { $x = $r.Left + $x }
445 + if ($y -eq $null) { $y = [int] (($r.Top + $r.Bottom) / 2) } else { $y = $r.Top + $y }
447 + if ($x -eq $null -or $y -eq $null) { throw 'click needs -X and -Y unless targeting a window center' }
448 + Invoke-RacingGuard $opts $x $y
449 + [void] [Win32DriveNative]::SetCursorPos($x, $y)
450 + $down = if ($button -eq 'right') { 0x0008 } else { 0x0002 }
451 + $up = if ($button -eq 'right') { 0x0010 } else { 0x0004 }
452 + for ($i = 0; $i -lt $count; $i++) {
453 + [Win32DriveNative]::mouse_event($down, 0, 0, 0, [UIntPtr]::Zero)
454 + Start-Sleep -Milliseconds 45
455 + [Win32DriveNative]::mouse_event($up, 0, 0, 0, [UIntPtr]::Zero)
456 + Start-Sleep -Milliseconds 80
458 + return [pscustomobject] [ordered] @{ ok = $true; x = $x; y = $y; button = $button; count = $count }
461 + $text = Str-Opt $opts 'Text'
462 + if ($null -eq $text) { throw 'type needs -Text' }
463 + if ($opts.ContainsKey('Hwnd') -or $opts.ContainsKey('Process') -or $opts.ContainsKey('TitleContains')) { [void] [Win32DriveNative]::SetForegroundWindow((Resolve-Window $opts)) }
464 + Invoke-RacingGuard $opts
465 + [Windows.Forms.SendKeys]::SendWait((Escape-SendKeysText $text))
466 + return [pscustomobject] [ordered] @{ ok = $true; chars = $text.Length }
469 + $keySpec = Str-Opt $opts 'Keys'
470 + if (-not $keySpec) { throw 'key needs -Keys' }
471 + if ($opts.ContainsKey('Hwnd') -or $opts.ContainsKey('Process') -or $opts.ContainsKey('TitleContains')) { [void] [Win32DriveNative]::SetForegroundWindow((Resolve-Window $opts)) }
472 + Invoke-RacingGuard $opts
473 + [Windows.Forms.SendKeys]::SendWait($keySpec)
474 + return [pscustomobject] [ordered] @{ ok = $true; keys = $keySpec }
477 + $action = (Str-Opt $opts 'Action').ToLowerInvariant()
478 + if (-not $action) { throw 'window needs -Action' }
479 + $hwnd = Resolve-Window $opts
480 + Invoke-RacingGuard $opts
483 + $r = Get-Rect $hwnd
484 + $x = Int-Opt $opts 'X' $r.Left
485 + $y = Int-Opt $opts 'Y' $r.Top
486 + $w = Int-Opt $opts 'Width' ($r.Right - $r.Left)
487 + $h = Int-Opt $opts 'Height' ($r.Bottom - $r.Top)
488 + [void] [Win32DriveNative]::MoveWindow($hwnd, $x, $y, $w, $h, $true)
490 + 'min' { [void] [Win32DriveNative]::ShowWindow($hwnd, 6) }
491 + 'max' { [void] [Win32DriveNative]::ShowWindow($hwnd, 3) }
492 + 'restore' { [void] [Win32DriveNative]::ShowWindow($hwnd, 9) }
493 + 'close' { [void] [Win32DriveNative]::PostMessage($hwnd, 0x0010, [IntPtr]::Zero, [IntPtr]::Zero) }
494 + default { throw "Unknown window action: $action" }
496 + return [pscustomobject] [ordered] @{ ok = $true; hwnd = $hwnd.ToInt64(); action = $action }
498 + default { throw "Unknown command: $Command" }
503 + Write-Json (Invoke-CommandImpl)
506 + Write-Json ([pscustomobject] [ordered] @{ ok = $false; error = $_.Exception.Message; command = $Command })