diff --git a/.github/workflows/build-signed.yml b/.github/workflows/build-signed.yml new file mode 100644 index 00000000..da6175e6 --- /dev/null +++ b/.github/workflows/build-signed.yml @@ -0,0 +1,59 @@ +name: Build signed installer (self-signed) + +on: + workflow_dispatch: + push: + branches: [ "claude/target-window" ] + +permissions: + contents: read + +jobs: + build: + runs-on: windows-latest + steps: + - uses: actions/checkout@v7 + + - name: Setup WinApp CLI + uses: microsoft/setup-WinAppCli@v0.3 + + - name: Create the app package + run: winapp pack AuraClick\AuraClick.csproj -c Debug --arch x86 --arch x64 --arch arm64 --output AuraClick.msixbundle + + - name: Create certificate and sign + shell: pwsh + run: | + # Subject must match the Publisher in Package.appxmanifest + $subject = ([xml](Get-Content AuraClick\Package.appxmanifest)).Package.Identity.Publisher + $pwd = ConvertTo-SecureString -String "auraclick" -Force -AsPlainText + $cert = New-SelfSignedCertificate -Type Custom -Subject $subject -KeyUsage DigitalSignature ` + -FriendlyName "AuraClick mod" -CertStoreLocation "Cert:\CurrentUser\My" ` + -TextExtension @("2.5.29.37={text}1.3.6.1.5.5.7.3.3", "2.5.29.19={text}") + Export-PfxCertificate -Cert $cert -FilePath cert.pfx -Password $pwd | Out-Null + Export-Certificate -Cert $cert -FilePath AuraClick-mod.cer | Out-Null + $signtool = Get-ChildItem "${env:ProgramFiles(x86)}\Windows Kits\10\bin\*\x64\signtool.exe" | + Sort-Object FullName | Select-Object -Last 1 + & $signtool.FullName sign /fd SHA256 /f cert.pfx /p auraclick AuraClick.msixbundle + if ($LASTEXITCODE) { exit $LASTEXITCODE } + Remove-Item cert.pfx + + - name: Write install script + shell: pwsh + run: | + @' + # Right-click > Run with PowerShell. Needs Administrator (it trusts the certificate once). + $dir = Split-Path -Parent $MyInvocation.MyCommand.Path + Import-Certificate -FilePath "$dir\AuraClick-mod.cer" -CertStoreLocation Cert:\LocalMachine\TrustedPeople | Out-Null + Add-AppxPackage -Path "$dir\AuraClick.msixbundle" + Write-Host "Installed. Find 'Aura Click' in the Start menu." + '@ | Set-Content Install.ps1 + + - name: Upload installer + uses: actions/upload-artifact@v7 + with: + name: AuraClick-mod-installer + path: | + AuraClick.msixbundle + AuraClick-mod.cer + Install.ps1 + if-no-files-found: error diff --git a/AuraClick/Helpers/AutoClicker.cs b/AuraClick/Helpers/AutoClicker.cs index e7eec5da..84693e3a 100644 --- a/AuraClick/Helpers/AutoClicker.cs +++ b/AuraClick/Helpers/AutoClicker.cs @@ -78,6 +78,12 @@ public static class AutoClicker /// public static int ClickDelayOffset { get; set; } = 10; + /// + /// Gets or sets the handle of the window to click in, or 0 to click wherever the cursor is. + /// When set, the window is brought to the front for each click and the previous window is restored afterwards. + /// + public static nint TargetWindow { get; set; } + /// /// Gets a value indicating whether the auto clicker is currently running. /// @@ -95,12 +101,35 @@ public static void Start() long clickLimit = ClickAmountEnabled ? ClickAmount : long.MaxValue; TimeSpan delay = TimeSpan.FromHours(HoursDelay, MinutesDelay, SecondsDelay, MillisecondsDelay); int maxOffset = ClickDelayOffsetEnabled ? Math.Max(0, ClickDelayOffset) : 0; + nint target = TargetWindow; _ = Task.Run(async () => { for (long clicks = 1; !cts.IsCancellationRequested; clicks++) { - _ = PInvoke.SendInput(click, Marshal.SizeOf()); + if (target != 0) + { + if (!WindowHelper.Exists(target)) + { + // Target window was closed + cts.Cancel(); + ClickLimitReached?.Invoke(null, EventArgs.Empty); + return; + } + + // Games ignore background input, so focus the window, press, hold briefly, release, then go back + nint previous = WindowHelper.BringToFront(target); + await Task.Delay(80).ConfigureAwait(false); + _ = PInvoke.SendInput(click.AsSpan(0, 1), Marshal.SizeOf()); + await Task.Delay(60).ConfigureAwait(false); + _ = PInvoke.SendInput(click.AsSpan(1, 1), Marshal.SizeOf()); + WindowHelper.Restore(previous, target); + } + else + { + _ = PInvoke.SendInput(click, Marshal.SizeOf()); + } + if (clicks >= clickLimit) { cts.Cancel(); diff --git a/AuraClick/Helpers/WindowHelper.cs b/AuraClick/Helpers/WindowHelper.cs new file mode 100644 index 00000000..cb4065e3 --- /dev/null +++ b/AuraClick/Helpers/WindowHelper.cs @@ -0,0 +1,164 @@ +using System.Runtime.InteropServices; +using System.Text; + +namespace AuraClick.Helpers; + +/// +/// A top-level window the auto clicker can target. +/// +public sealed class WindowEntry(nint handle, string title) +{ + /// + /// Gets the window handle. + /// + public nint Handle { get; } = handle; + + /// + /// Gets the window title. + /// + public string Title { get; } = title; + + /// + public override string ToString() => Title; +} + +/// +/// Helpers for listing top-level windows and bringing one to the foreground. +/// +public static class WindowHelper +{ + private const int SwRestore = 9; + private const int DwmaCloaked = 14; + + private delegate bool EnumWindowsProc(nint hWnd, nint lParam); + + [DllImport("user32.dll")] + private static extern bool EnumWindows(EnumWindowsProc callback, nint lParam); + + [DllImport("user32.dll")] + private static extern bool IsWindowVisible(nint hWnd); + + [DllImport("user32.dll")] + private static extern bool IsWindow(nint hWnd); + + [DllImport("user32.dll")] + private static extern bool IsIconic(nint hWnd); + + [DllImport("user32.dll")] + private static extern bool ShowWindow(nint hWnd, int nCmdShow); + + [DllImport("user32.dll")] + private static extern nint GetForegroundWindow(); + + [DllImport("user32.dll")] + private static extern bool SetForegroundWindow(nint hWnd); + + [DllImport("user32.dll")] + private static extern uint GetWindowThreadProcessId(nint hWnd, out uint processId); + + [DllImport("user32.dll")] + private static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach); + + [DllImport("user32.dll", CharSet = CharSet.Unicode)] + private static extern int GetWindowTextLength(nint hWnd); + + [DllImport("user32.dll", CharSet = CharSet.Unicode)] + private static extern int GetWindowText(nint hWnd, StringBuilder text, int maxCount); + + [DllImport("kernel32.dll")] + private static extern uint GetCurrentThreadId(); + + [DllImport("dwmapi.dll")] + private static extern int DwmGetWindowAttribute(nint hWnd, int attribute, out int value, int size); + + /// + /// Lists visible, titled top-level windows, excluding . + /// + public static List GetOpenWindows(nint exclude) + { + List result = []; + _ = EnumWindows((hWnd, _) => + { + if (hWnd == exclude || !IsWindowVisible(hWnd)) + { + return true; + } + + // Skip hidden UWP shells that report as visible but are cloaked + if (DwmGetWindowAttribute(hWnd, DwmaCloaked, out int cloaked, sizeof(int)) == 0 && cloaked != 0) + { + return true; + } + + int length = GetWindowTextLength(hWnd); + if (length <= 0) + { + return true; + } + + StringBuilder sb = new(length + 1); + _ = GetWindowText(hWnd, sb, sb.Capacity); + string title = sb.ToString().Trim(); + if (title.Length > 0) + { + result.Add(new WindowEntry(hWnd, title)); + } + + return true; + }, 0); + + result.Sort((a, b) => string.Compare(a.Title, b.Title, StringComparison.OrdinalIgnoreCase)); + return result; + } + + /// + /// Returns whether the window still exists. + /// + public static bool Exists(nint hWnd) => hWnd != 0 && IsWindow(hWnd); + + /// + /// Brings the window to the foreground and returns the previously active window. + /// + public static nint BringToFront(nint target) + { + nint previous = GetForegroundWindow(); + if (previous == target) + { + return previous; + } + + if (IsIconic(target)) + { + _ = ShowWindow(target, SwRestore); + } + + ForceForeground(target, previous); + return previous; + } + + /// + /// Returns focus to the window that was active before . + /// + public static void Restore(nint previous, nint target) + { + if (previous != 0 && previous != target && IsWindow(previous)) + { + ForceForeground(previous, GetForegroundWindow()); + } + } + + private static void ForceForeground(nint window, nint currentForeground) + { + uint foregroundThread = currentForeground != 0 ? GetWindowThreadProcessId(currentForeground, out _) : 0; + uint thisThread = GetCurrentThreadId(); + bool attached = foregroundThread != 0 && foregroundThread != thisThread + && AttachThreadInput(thisThread, foregroundThread, true); + + _ = SetForegroundWindow(window); + + if (attached) + { + _ = AttachThreadInput(thisThread, foregroundThread, false); + } + } +} diff --git a/AuraClick/Pages/MainPage.xaml b/AuraClick/Pages/MainPage.xaml index be2fe002..b2f31250 100644 --- a/AuraClick/Pages/MainPage.xaml +++ b/AuraClick/Pages/MainPage.xaml @@ -1,4 +1,4 @@ - + + @@ -193,6 +194,29 @@ IsEnabled="{x:Bind (x:Boolean)ClickOffsetCheckBox.IsChecked}" /> + + + + + + + + + + +