$ErrorActionPreference = 'Stop' [Console]::InputEncoding = New-Object System.Text.UTF8Encoding($false) [Console]::OutputEncoding = New-Object System.Text.UTF8Encoding($false) Add-Type -Path (Join-Path $PSScriptRoot 'windows.cs') -ReferencedAssemblies System,System.Core,System.Drawing [PiDesktop]::Initialize() try { while ($null -ne ($line = [Console]::ReadLine())) { try { $request = $line | ConvertFrom-Json $response = @{} switch ($request.action) { 'pointer' { [PiDesktop]::Pointer($request.x, $request.y, [string[]]$request.buttons) } 'scroll' { $ticks = if ($null -eq $request.ticks) { 1 } else { $request.ticks } [PiDesktop]::Scroll($request.x, $request.y, $request.direction, $ticks, [string[]]$request.buttons) } 'type' { [PiDesktop]::TypeText($request.text) } 'key' { [PiDesktop]::PressKey($request.key, [string[]]$request.modifiers) } 'new_browser_tab' { [PiDesktop]::OpenBrowser($request.url) } 'screenshot' { $response.png = [PiDesktop]::Screenshot() } default { throw "Unsupported desktop action: $($request.action)" } } [Console]::WriteLine(($response | ConvertTo-Json -Compress)) } catch { [Console]::WriteLine((@{ error = $_.Exception.GetBaseException().Message } | ConvertTo-Json -Compress)) break } } } finally { [PiDesktop]::Release() }