From 68a30e04859795a654582cd0a806deb3cf4439f0 Mon Sep 17 00:00:00 2001 From: Ashleigh Adams Date: Thu, 2 Oct 2025 02:51:18 +0100 Subject: [PATCH] Yet more attempts to harden window focusing Occasionally, these still don't result in focus, so try more things to force the focus to be set. --- src/Keyden.Desktop.Windows/Win32.cs | 6 ++++++ src/Keyden.Desktop.Windows/WindowsSystemServices.cs | 4 +++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Keyden.Desktop.Windows/Win32.cs b/src/Keyden.Desktop.Windows/Win32.cs index 06b1615..afef20b 100644 --- a/src/Keyden.Desktop.Windows/Win32.cs +++ b/src/Keyden.Desktop.Windows/Win32.cs @@ -31,6 +31,12 @@ public struct Rect [return: MarshalAs(UnmanagedType.Bool)] public static partial bool SetForegroundWindow(nint hWnd); + [LibraryImport("user32.dll", EntryPoint = "SetFocus")] + public static partial nint SetFocus(nint hWnd); + + [LibraryImport("user32.dll", EntryPoint = "SetActiveWindow")] + public static partial nint SetActiveWindow(nint hWnd); + public const uint CONST_SW_SHOW = 5; [LibraryImport("user32.dll", EntryPoint = "ShowWindow")] diff --git a/src/Keyden.Desktop.Windows/WindowsSystemServices.cs b/src/Keyden.Desktop.Windows/WindowsSystemServices.cs index ad46119..4b161e8 100644 --- a/src/Keyden.Desktop.Windows/WindowsSystemServices.cs +++ b/src/Keyden.Desktop.Windows/WindowsSystemServices.cs @@ -207,9 +207,11 @@ public async Task TryAuthUser(AuthRequired authRequired, ClientInfo var currentThreadId = Win32.GetCurrentThreadId(); Win32.AttachThreadInput(windowThreadProcessId, currentThreadId, true); Win32.BringWindowToTop(windowHwnd); + Win32.SetForegroundWindow(windowHwnd); + Win32.SetFocus(windowHwnd); + Win32.SetActiveWindow(windowHwnd); Win32.ShowWindow(windowHwnd, Win32.CONST_SW_SHOW); Win32.AttachThreadInput(windowThreadProcessId, currentThreadId, false); - //Win32.SetForegroundWindow(windowHwnd); } result = await window.Result;