From 512120db04fe4ba909dde3016688277a652cd735 Mon Sep 17 00:00:00 2001 From: mqudsi Date: Tue, 10 Jun 2025 22:52:08 -0500 Subject: [PATCH] Work around Escape hotkey race with exit confirmation dialog See merge request ryubing/ryujinx!54 --- src/Ryujinx/Systems/AppHost.cs | 18 ++++++++++++++++++ src/Ryujinx/UI/Helpers/Win32NativeInterop.cs | 3 +++ 2 files changed, 21 insertions(+) diff --git a/src/Ryujinx/Systems/AppHost.cs b/src/Ryujinx/Systems/AppHost.cs index 7089479d3..74005400c 100644 --- a/src/Ryujinx/Systems/AppHost.cs +++ b/src/Ryujinx/Systems/AppHost.cs @@ -1152,6 +1152,24 @@ namespace Ryujinx.Ava.Systems _dialogShown = true; + // The hard-coded hotkey mapped to exit is Escape, but it's also the same key + // that causes the dialog we launch to close (without doing anything). In release + // mode, a race is observed that between ShowExitPrompt() appearing on KeyDown + // and the ContentDialog we create seeing the key state before KeyUp. Merely waiting + // for the key to no longer be pressed appears to be insufficient. + // NB: Using _keyboardInterface.IsPressed(Key.Escape) does not currently work. + if (OperatingSystem.IsWindows()) + { + while (GetAsyncKeyState(0x1B) != 0) + { + await Task.Delay(100); + } + } + else + { + await Task.Delay(250); + } + shouldExit = await ContentDialogHelper.CreateStopEmulationDialog(); _dialogShown = false; diff --git a/src/Ryujinx/UI/Helpers/Win32NativeInterop.cs b/src/Ryujinx/UI/Helpers/Win32NativeInterop.cs index d7466e95b..c7d217bf5 100644 --- a/src/Ryujinx/UI/Helpers/Win32NativeInterop.cs +++ b/src/Ryujinx/UI/Helpers/Win32NativeInterop.cs @@ -110,5 +110,8 @@ namespace Ryujinx.Ava.UI.Helpers [LibraryImport("user32.dll", SetLastError = true)] public static partial nint SetWindowLongPtrW(nint hWnd, int nIndex, nint value); + + [LibraryImport("user32.dll", SetLastError = true)] + public static partial ushort GetAsyncKeyState(int nVirtKey); } }