diff --git a/src/Avalonia.Input/AccessKeyHandler.cs b/src/Avalonia.Input/AccessKeyHandler.cs index 7acbc109fc..7baa4103d7 100644 --- a/src/Avalonia.Input/AccessKeyHandler.cs +++ b/src/Avalonia.Input/AccessKeyHandler.cs @@ -43,6 +43,16 @@ namespace Avalonia.Input /// private bool _ignoreAltUp; + /// + /// Whether the AltKey is down. + /// + private bool _altIsDown; + + /// + /// Element to restore folowing AltKey taking focus. + /// + private IInputElement _restoreFocusElement; + /// /// Gets or sets the window's main menu. /// @@ -110,8 +120,14 @@ namespace Avalonia.Input { if (e.Key == Key.LeftAlt) { + _altIsDown = true; + if (MainMenu == null || !MainMenu.IsOpen) { + // TODO: Use FocusScopes to store the current element and restore it when context menu is closed. + // Save currently focused input element. + _restoreFocusElement = FocusManager.Instance.Current; + // When Alt is pressed without a main menu, or with a closed main menu, show // access key markers in the window (i.e. "_File"). _owner.ShowAccessKeys = _showingAccessKeys = true; @@ -121,11 +137,18 @@ namespace Avalonia.Input // If the Alt key is pressed and the main menu is open, close the main menu. CloseMenu(); _ignoreAltUp = true; + + _restoreFocusElement?.Focus(); + _restoreFocusElement = null; } // We always handle the Alt key. e.Handled = true; } + else if (_altIsDown) + { + _ignoreAltUp = true; + } } /// @@ -179,6 +202,8 @@ namespace Avalonia.Input switch (e.Key) { case Key.LeftAlt: + _altIsDown = false; + if (_ignoreAltUp) { _ignoreAltUp = false;