Something went wrong. Try again.
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546param([switch]$Once)$ErrorActionPreference = 'Stop'[Console]::OutputEncoding = New-Object System.Text.UTF8Encoding($false)Add-Type -TypeDefinition @'using System;using System.Runtime.InteropServices;public static class GhostXbox { [StructLayout(LayoutKind.Sequential)] public struct Pad { public ushort buttons; public byte leftTrigger, rightTrigger; public short leftX, leftY, rightX, rightY; } [StructLayout(LayoutKind.Sequential)] public struct State { public uint packet; public Pad pad; } [DllImport("xinput1_4.dll", EntryPoint="XInputGetState")] public static extern uint GetState(uint index, out State state);}'@$previous = ''$lastSent = [DateTime]::UtcNowdo { $found = $false for ($slot = 0; $slot -lt 4; $slot++) { $state = New-Object GhostXbox+State if ([GhostXbox]::GetState($slot, [ref]$state) -eq 0) { $found = $true $frame = [ordered]@{ connected = $true; index = $slot; packet = $state.packet buttons = $state.pad.buttons leftTrigger = $state.pad.leftTrigger; rightTrigger = $state.pad.rightTrigger leftX = $state.pad.leftX; leftY = $state.pad.leftY rightX = $state.pad.rightX; rightY = $state.pad.rightY } break } } if (-not $found) { $frame = [ordered]@{ connected = $false } } $line = ConvertTo-Json -InputObject $frame -Compress if ($Once -or $line -ne $previous -or ([DateTime]::UtcNow - $lastSent).TotalSeconds -ge 1) { [Console]::WriteLine($line) $previous = $line; $lastSent = [DateTime]::UtcNow } if (-not $Once) { Start-Sleep -Milliseconds 16 }} while (-not $Once)