Repository: AvaloniaUI/Avalonia

Issue:
Duplicate Accelerator Keys don't work for nested MenuItem Headers
**Describe the bug**

```xaml
<Menu>
    <MenuItem Header="_File">
        <MenuItem Header="_New Project"/>
        <MenuItem Header="_Open Project"/>
        <MenuItem Header="_Save Project"/>
    </MenuItem>
    <MenuItem Header="_Edit">
        <MenuItem Header="_Find"/>
        <MenuItem Header="_Replace"/>
    </MenuItem>
</Menu>
```

This is a very simple Menu that you can find in most editor-like applications. This is how I expect it to work:

![Rider](https://user-images.githubusercontent.com/16577545/145012338-3b5002dc-b673-489c-82bb-6529e21ef23c.gif)

Both `File` and `Find` start with `F` but they are not on the same level, `Find` is nested inside the `Edit` menu meaning you have to do `Alt+E+F` to get to `Find`. Avalonia does not work this way:

![Avalonia](https://user-images.githubusercontent.com/16577545/145012904-45532663-3a1a-4832-8e5f-3f78068ad860.gif)

As you can see, if the `Edit` menu is open and I press `F`, I expect it to select the `Find` sub-menu item but instead it goes to `File`.

**Desktop (please complete the following information):**

- OS: Windows 10
- Version 0.10.10



Hints:
Looking a bit around in the code I guess you'd have to change the [`MenuItemAccessKeyHandler`](https://github.com/AvaloniaUI/Avalonia/blob/master/src/Avalonia.Controls/MenuItemAccessKeyHandler.cs) and [`AccessText`](https://github.com/AvaloniaUI/Avalonia/blob/master/src/Avalonia.Controls/Primitives/AccessText.cs) to be aware of what parent `MenuItem` they are in as they currently only work with `char accessKey`:

https://github.com/AvaloniaUI/Avalonia/blob/0ca2f3d5db0d4c5a5a35782e396d40e2ac98f87a/src/Avalonia.Controls/MenuItemAccessKeyHandler.cs#L58-L68
I noticed that this is still a bug in Avalonia - is there a work-around for this?

Patch:
diff --git a/api/Avalonia.nupkg.xml b/api/Avalonia.nupkg.xml
index 1edc3b5ec25..f6797ada377 100644
--- a/api/Avalonia.nupkg.xml
+++ b/api/Avalonia.nupkg.xml
@@ -91,4 +91,10 @@
     <Left>baseline/netstandard2.0/Avalonia.Controls.dll</Left>
     <Right>target/netstandard2.0/Avalonia.Controls.dll</Right>
   </Suppression>
+  <Suppression>
+    <DiagnosticId>CP0012</DiagnosticId>
+    <Target>M:Avalonia.Controls.Button.OnAccessKey(Avalonia.Interactivity.RoutedEventArgs)</Target>
+    <Left>baseline/netstandard2.0/Avalonia.Controls.dll</Left>
+    <Right>target/netstandard2.0/Avalonia.Controls.dll</Right>
+  </Suppression>
 </Suppressions>
\ No newline at end of file
diff --git a/samples/ControlCatalog/MainView.xaml b/samples/ControlCatalog/MainView.xaml
index 6246c73ce2c..e3eed5fb0e3 100644
--- a/samples/ControlCatalog/MainView.xaml
+++ b/samples/ControlCatalog/MainView.xaml
@@ -18,6 +18,9 @@
       <TabItem Header="Composition">
         <pages:CompositionPage />
       </TabItem>
+      <TabItem Header="Accelerator">
+        <pages:AcceleratorPage />
+      </TabItem>
       <TabItem Header="Acrylic">
         <pages:AcrylicPage />
       </TabItem>
diff --git a/samples/ControlCatalog/Pages/AcceleratorPage.xaml b/samples/ControlCatalog/Pages/AcceleratorPage.xaml
new file mode 100644
index 00000000000..18072ef1c3d
--- /dev/null
+++ b/samples/ControlCatalog/Pages/AcceleratorPage.xaml
@@ -0,0 +1,115 @@
+<UserControl xmlns="https://github.com/avaloniaui"
+             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+             x:Class="ControlCatalog.Pages.AcceleratorPage">
+  <StackPanel Orientation="Vertical" Spacing="4">
+
+    <WrapPanel HorizontalAlignment="Left">
+      <StackPanel>
+        <Menu>
+          <MenuItem Header="_First">
+            <MenuItem Header="Standard _Menu Item" InputGesture="Ctrl+A" />
+            <MenuItem Header="_Disabled Menu Item" IsEnabled="False" InputGesture="Ctrl+D" />
+            <Separator />
+            <MenuItem Header="Menu with Sub _Menu">
+              <MenuItem Header="Submenu _1" />
+              <MenuItem Header="Submenu _2 with Submenu">
+                <MenuItem Header="Submenu Level 2" />
+              </MenuItem>
+              <MenuItem Header="Submenu _3 with Submenu Disabled" IsEnabled="False">
+                <MenuItem Header="Submenu Level 2" />
+              </MenuItem>
+            </MenuItem>
+            <MenuItem Header="Menu Item with _Icon" InputGesture="Ctrl+Shift+B">
+              <MenuItem.Icon>
+                <Image Source="/Assets/github_icon.png" />
+              </MenuItem.Icon>
+            </MenuItem>
+            <MenuItem Header="Menu Item with _Checkbox" ToggleType="CheckBox" />
+          </MenuItem>
+          <MenuItem Header="_Second">
+            <MenuItem Header="Second _Menu Item" />
+            <MenuItem IsChecked="True" Header="Second _Menu toggle item" ToggleType="CheckBox" />
+            <Separator />
+            <MenuItem GroupName="A" Header="Radio 1 - group" ToggleType="Radio" />
+            <MenuItem IsChecked="True" GroupName="A" Header="Radio 2 - group" ToggleType="Radio" />
+            <MenuItem GroupName="A" Header="Radio 3 - group" ToggleType="Radio">
+              <MenuItem Header="Radio 4 - group" ToggleType="Radio" GroupName="A" />
+              <MenuItem Header="Radio 5 - group" ToggleType="Radio" GroupName="A" />
+            </MenuItem>
+            <Separator />
+            <MenuItem Header="Radio 1" ToggleType="Radio" />
+            <MenuItem IsChecked="True" Header="Radio 2" ToggleType="Radio" />
+            <MenuItem Header="Radio 3" ToggleType="Radio">
+              <MenuItem Header="Radio 4" ToggleType="Radio" />
+              <MenuItem Header="Radio 5" ToggleType="Radio" />
+            </MenuItem>
+          </MenuItem>
+          <MenuItem Header="Thir_d">
+            <MenuItem Header="About"/>
+            <MenuItem Header="_Child">
+              <MenuItem Header="_Grandchild"/>
+            </MenuItem>
+          </MenuItem>
+        </Menu>
+      </StackPanel>
+
+    </WrapPanel>
+
+    <StackPanel Spacing="10">
+      <TextBlock Classes="h2">Accelerator Support</TextBlock>
+
+      <TabControl Margin="10" BorderBrush="Gray" BorderThickness="1">
+        <TabItem Header="_Tab 1">
+          <StackPanel>
+            <TextBlock Margin="5">This is tab 1 content</TextBlock>
+            <Label Name="Tab1Label1" Target="Tab1TextBox1">_Label Tab1Label1</Label>
+            <TextBox Name="Tab1TextBox1" Margin="5">This is tab 1 content</TextBox>
+            <Label Name="Tab1Label2" Target="Tab1TextBox2">Label _Tab1Label2</Label>
+            <TextBox Name="Tab1TextBox2" Margin="5">This is tab 1 content</TextBox>
+          </StackPanel>
+
+        </TabItem>
+        <TabItem Header="T_ab 2">
+          <TextBlock Margin="5">This is tab 2 content</TextBlock>
+        </TabItem>
+        <TabItem Header="_Tab 3">
+
+        </TabItem>
+        <TabItem Header="_Tab 4">
+          <TextBlock Margin="5">This is tab 4 content</TextBlock>
+        </TabItem>
+        <TabItem Header="_Fab 5">
+          <TextBlock Margin="5">This is fab 5 content</TextBlock>
+        </TabItem>
+      </TabControl>
+    </StackPanel>
+
+
+    <StackPanel Spacing="10">
+      <Label Name="Label0">Label with Ac_celerator 'C' and no Target</Label>
+      <TextBox Name="TextBox0" Text="Some Text"></TextBox>
+
+      <Label Name="Label1" Target="TextBox1">_Label with Accelerator 'L'</Label>
+      <TextBox Name="TextBox1" Text="Some Text"></TextBox>
+
+      <Label Name="Label2" Target="TextBox2">La_bel with Accelerator 'B'</Label>
+      <TextBox Name="TextBox2" Text="Some Text"></TextBox>
+
+      <Label Name="Label3" Target="TextBox3">L_abel with Accelerator 'A'</Label>
+      <TextBox Name="TextBox3" Text="Some Text"></TextBox>
+
+      <Label Name="Label4" Target="TextBox4">La_bel with Accelerator 'B'</Label>
+      <TextBox Name="TextBox4" Text="Some Text"></TextBox>
+
+      <Label Name="Label5" Target="TextBox5">_Flabel with Accelerator 'F' (Same as in Menu > File)</Label>
+      <TextBox Name="TextBox5" Text="Some Text"></TextBox>
+
+    </StackPanel>
+
+    <StackPanel Spacing="10" Orientation="Horizontal">
+      <Button Name="Button1">_Button 1</Button>
+      <Button Name="Button2">_Button 2</Button>
+      <Button Name="Button3">_Button 3</Button>
+    </StackPanel>
+  </StackPanel>
+</UserControl>
diff --git a/samples/ControlCatalog/Pages/AcceleratorPage.xaml.cs b/samples/ControlCatalog/Pages/AcceleratorPage.xaml.cs
new file mode 100644
index 00000000000..0d0d684700c
--- /dev/null
+++ b/samples/ControlCatalog/Pages/AcceleratorPage.xaml.cs
@@ -0,0 +1,18 @@
+using Avalonia.Controls;
+using Avalonia.Markup.Xaml;
+
+namespace ControlCatalog.Pages
+{
+    public class AcceleratorPage : UserControl
+    {
+        public AcceleratorPage()
+        {
+            this.InitializeComponent();
+        }
+
+        private void InitializeComponent()
+        {
+            AvaloniaXamlLoader.Load(this);
+        }
+    }
+}
diff --git a/src/Avalonia.Base/Input/AccessKeyHandler.cs b/src/Avalonia.Base/Input/AccessKeyHandler.cs
index fe5e2c46a21..96a4847db8e 100644
--- a/src/Avalonia.Base/Input/AccessKeyHandler.cs
+++ b/src/Avalonia.Base/Input/AccessKeyHandler.cs
@@ -3,6 +3,7 @@
 using System.Linq;
 using Avalonia.Interactivity;
 using Avalonia.LogicalTree;
+using Avalonia.VisualTree;
 
 namespace Avalonia.Input
 {
@@ -11,11 +12,20 @@ namespace Avalonia.Input
     /// </summary>
     internal class AccessKeyHandler : IAccessKeyHandler
     {
+        /// <summary>
+        /// Defines the AccessKey attached event.
+        /// </summary>
+        public static readonly RoutedEvent<AccessKeyEventArgs> AccessKeyEvent =
+            RoutedEvent.Register<AccessKeyEventArgs>(
+                "AccessKey",
+                RoutingStrategies.Bubble,
+                typeof(AccessKeyHandler));
+
         /// <summary>
         /// Defines the AccessKeyPressed attached event.
         /// </summary>
-        public static readonly RoutedEvent<RoutedEventArgs> AccessKeyPressedEvent =
-            RoutedEvent.Register<RoutedEventArgs>(
+        public static readonly RoutedEvent<AccessKeyPressedEventArgs> AccessKeyPressedEvent =
+            RoutedEvent.Register<AccessKeyPressedEventArgs>(
                 "AccessKeyPressed",
                 RoutingStrategies.Bubble,
                 typeof(AccessKeyHandler));
@@ -23,7 +33,9 @@ internal class AccessKeyHandler : IAccessKeyHandler
         /// <summary>
         /// The registered access keys.
         /// </summary>
-        private readonly List<(string AccessKey, IInputElement Element)> _registered = new();
+        private readonly List<AccessKeyRegistration> _registrations = [];
+
+        protected IReadOnlyList<AccessKeyRegistration> Registrations => _registrations;
 
         /// <summary>
         /// The window to which the handler belongs.
@@ -48,7 +60,7 @@ internal class AccessKeyHandler : IAccessKeyHandler
         /// <summary>
         /// Element to restore following AltKey taking focus.
         /// </summary>
-        private IInputElement? _restoreFocusElement;
+        private WeakReference<IInputElement>? _restoreFocusElementRef;
 
         /// <summary>
         /// The window's main menu.
@@ -97,6 +109,12 @@ public void SetOwner(IInputRoot owner)
             _owner.AddHandler(InputElement.KeyDownEvent, OnKeyDown, RoutingStrategies.Bubble);
             _owner.AddHandler(InputElement.KeyUpEvent, OnPreviewKeyUp, RoutingStrategies.Tunnel);
             _owner.AddHandler(InputElement.PointerPressedEvent, OnPreviewPointerPressed, RoutingStrategies.Tunnel);
+
+            OnSetOwner(owner);
+        }
+
+        protected virtual void OnSetOwner(IInputRoot owner)
+        {
         }
 
         /// <summary>
@@ -106,14 +124,19 @@ public void SetOwner(IInputRoot owner)
         /// <param name="element">The input element.</param>
         public void Register(char accessKey, IInputElement element)
         {
-            var existing = _registered.FirstOrDefault(x => x.Item2 == element);
-
-            if (existing != default)
+            var key = NormalizeKey(accessKey.ToString());
+            
+            // remove dead elements with matching key
+            for (var i = _registrations.Count - 1; i >= 0; i--)
             {
-                _registered.Remove(existing);
+                var registration = _registrations[i];
+                if (registration.Key == key && registration.GetInputElement() == null)
+                {
+                    _registrations.RemoveAt(i);    
+                }
             }
 
-            _registered.Add((accessKey.ToString().ToUpperInvariant(), element));
+            _registrations.Add(new AccessKeyRegistration(key, new WeakReference<IInputElement>(element)));
         }
 
         /// <summary>
@@ -122,9 +145,15 @@ public void Register(char accessKey, IInputElement element)
         /// <param name="element">The input element.</param>
         public void Unregister(IInputElement element)
         {
-            foreach (var i in _registered.Where(x => x.Item2 == element).ToList())
+            // remove element and all dead elements
+            for (var i = _registrations.Count - 1; i >= 0; i--)
             {
-                _registered.Remove(i);
+                var registration = _registrations[i];
+                var inputElement = registration.GetInputElement();
+                if (inputElement == null || inputElement == element)
+                {
+                    _registrations.RemoveAt(i);    
+                }
             }
         }
 
@@ -135,21 +164,29 @@ public void Unregister(IInputElement element)
         /// <param name="e">The event args.</param>
         protected virtual void OnPreviewKeyDown(object? sender, KeyEventArgs e)
         {
-            if (e.Key == Key.LeftAlt || e.Key == Key.RightAlt)
+            // if the owner (IInputRoot) does not have the keyboard focus, ignore all keyboard events
+            // KeyboardDevice.IsKeyboardFocusWithin in case of a PopupRoot seems to only work once, so we created our own
+            var isFocusWithinOwner = IsFocusWithinOwner(_owner!);
+            if (!isFocusWithinOwner)
+                return;
+
+            if (e.Key is Key.LeftAlt or Key.RightAlt)
             {
                 _altIsDown = true;
 
-                if (MainMenu == null || !MainMenu.IsOpen)
+                if (MainMenu is not { IsOpen: true })
                 {
                     var focusManager = FocusManager.GetFocusManager(e.Source as IInputElement);
-                    
+
                     // TODO: Use FocusScopes to store the current element and restore it when context menu is closed.
                     // Save currently focused input element.
-                    _restoreFocusElement = focusManager?.GetFocusedElement();
+                    var focusedElement = focusManager?.GetFocusedElement();
+                    if (focusedElement is not null)
+                        _restoreFocusElementRef = new WeakReference<IInputElement>(focusedElement);
 
                     // 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;
+                    _owner!.ShowAccessKeys = _showingAccessKeys = isFocusWithinOwner;
                 }
                 else
                 {
@@ -157,8 +194,11 @@ protected virtual void OnPreviewKeyDown(object? sender, KeyEventArgs e)
                     CloseMenu();
                     _ignoreAltUp = true;
 
-                    _restoreFocusElement?.Focus();
-                    _restoreFocusElement = null;
+                    if (_restoreFocusElementRef?.TryGetTarget(out var restoreElement) ?? false)
+                    {
+                        restoreElement.Focus();
+                    }
+                    _restoreFocusElementRef = null;
                 }
             }
             else if (_altIsDown)
@@ -174,35 +214,20 @@ protected virtual void OnPreviewKeyDown(object? sender, KeyEventArgs e)
         /// <param name="e">The event args.</param>
         protected virtual void OnKeyDown(object? sender, KeyEventArgs e)
         {
-            bool menuIsOpen = MainMenu?.IsOpen == true;
+            // if the owner (IInputRoot) does not have the keyboard focus, ignore all keyboard events
+            // KeyboardDevice.IsKeyboardFocusWithin in case of a PopupRoot seems to only work once, so we created our own
+            var isFocusWithinOwner = IsFocusWithinOwner(_owner!);
+            if (!isFocusWithinOwner)
+                return;
 
-            if (e.KeyModifiers.HasAllFlags(KeyModifiers.Alt) && !e.KeyModifiers.HasAllFlags(KeyModifiers.Control) || menuIsOpen)
-            {
-                // If any other key is pressed with the Alt key held down, or the main menu is open,
-                // find all controls who have registered that access key.
-                var text = e.Key.ToString();
-                var matches = _registered
-                    .Where(x => string.Equals(x.AccessKey, text, StringComparison.OrdinalIgnoreCase)
-                        && x.Element.IsEffectivelyVisible
-                        && x.Element.IsEffectivelyEnabled)
-                    .Select(x => x.Element);
-
-                // If the menu is open, only match controls in the menu's visual tree.
-                if (menuIsOpen)
-                {
-                    matches = matches.Where(x => x is not null && ((Visual)MainMenu!).IsLogicalAncestorOf((Visual)x));
-                }
-
-                var match = matches.FirstOrDefault();
+            if ((!e.KeyModifiers.HasAllFlags(KeyModifiers.Alt) || e.KeyModifiers.HasAllFlags(KeyModifiers.Control)) &&
+                MainMenu?.IsOpen != true)
+                return;
 
-                // If there was a match, raise the AccessKeyPressed event on it.
-                if (match is not null)
-                {
-                    match.RaiseEvent(new RoutedEventArgs(AccessKeyPressedEvent));
-                }
-            }
+            e.Handled = ProcessKey(e.Key.ToString(), e.Source as IInputElement);
         }
 
+
         /// <summary>
         /// Handles the Alt/F10 keys being released in the window.
         /// </summary>
@@ -255,5 +280,302 @@ private void MainMenuClosed(object? sender, EventArgs e)
         {
             _owner!.ShowAccessKeys = false;
         }
+
+        /// <summary>
+        /// Processes the given key for the element's targets 
+        /// </summary>
+        /// <param name="key">The access key to process.</param>
+        /// <param name="element">The element to get the targets which are in scope.</param>
+        /// <returns>If there matches <c>true</c>, otherwise <c>false</c>.</returns>
+        protected bool ProcessKey(string key, IInputElement? element)
+        {
+            key = NormalizeKey(key);
+            var senderInfo = GetTargetForElement(element, key);
+            // Find the possible targets matching the access key
+            var targets = SortByHierarchy(GetTargetsForKey(key, element, senderInfo));
+            var result = ProcessKey(key, targets);
+            return result != ProcessKeyResult.NoMatch;
+        }
+
+        private static string NormalizeKey(string key) => key.ToUpperInvariant();
+
+        private static ProcessKeyResult ProcessKey(string key, List<IInputElement> targets)
+        {
+            if (!targets.Any())
+                return ProcessKeyResult.NoMatch;
+
+            var isSingleTarget = true;
+            var lastWasFocused = false;
+
+            IInputElement? effectiveTarget = null;
+
+            var chosenIndex = 0;
+            for (var i = 0; i < targets.Count; i++)
+            {
+                var target = targets[i];
+
+                if (!IsTargetable(target))
+                    continue;
+
+                if (effectiveTarget == null)
+                {
+                    effectiveTarget = target;
+                    chosenIndex = i;
+                }
+                else
+                {
+                    if (lastWasFocused)
+                    {
+                        effectiveTarget = target;
+                        chosenIndex = i;
+                    }
+
+                    isSingleTarget = false;
+                }
+
+                lastWasFocused = target.IsFocused;
+            }
+
+            if (effectiveTarget == null)
+                return ProcessKeyResult.NoMatch;
+
+            var args = new AccessKeyEventArgs(key, isMultiple: !isSingleTarget);
+            effectiveTarget.RaiseEvent(args);
+
+            return chosenIndex == targets.Count - 1 ? ProcessKeyResult.LastMatch : ProcessKeyResult.MoreMatches;
+        }
+
+        private List<IInputElement> GetTargetsForKey(string key, IInputElement? sender,
+            AccessKeyInformation senderInfo)
+        {
+            var possibleElements = CopyMatchingAndPurgeDead(key);
+
+            if (!possibleElements.Any())
+                return possibleElements;
+
+            var finalTargets = new List<IInputElement>(1);
+
+            // Go through all the possible elements, find the interesting candidates
+            foreach (var element in possibleElements)
+            {
+                if (element != sender)
+                {
+                    if (!IsTargetable(element))
+                        continue;
+
+                    var elementInfo = GetTargetForElement(element, key);
+                    if (elementInfo.Target == null)
+                        continue;
+
+                    finalTargets.Add(elementInfo.Target);
+                }
+                else
+                {
+                    // This is the same element that sent the event so it must be in the same scope.
+                    // Just add it to the final targets
+                    if (senderInfo.Target == null)
+                        continue;
+
+                    finalTargets.Add(senderInfo.Target);
+                }
+            }
+
+            return finalTargets;
+        }
+
+        private static bool IsTargetable(IInputElement element) =>
+            element is { IsEffectivelyEnabled: true, IsEffectivelyVisible: true };
+
+        private List<IInputElement> CopyMatchingAndPurgeDead(string key)
+        {
+            var matches = new List<IInputElement>(_registrations.Count);
+                
+            // collect live elements with matching key and remove dead elements
+            for (var i = _registrations.Count - 1; i >= 0; i--)
+            {
+                var registration = _registrations[i];
+                var inputElement = registration.GetInputElement();
+                if (inputElement != null)
+                {
+                    if (registration.Key == key)
+                    {
+                        matches.Add(inputElement);
+                    }
+                }
+                else
+                {
+                    _registrations.RemoveAt(i);
+                }
+            }
+
+            // since we collected the elements when iterating from back to front
+            // we need to reverse them to ensure the original order
+            matches.Reverse();
+            
+            return matches;
+        }
+
+        /// <summary>
+        /// Returns targeting information for the given element.
+        /// </summary>
+        /// <param name="element"></param>
+        /// <param name="key"></param>
+        /// <returns>AccessKeyInformation with target for the access key.</returns>
+        private static AccessKeyInformation GetTargetForElement(IInputElement? element, string key)
+        {
+            var info = new AccessKeyInformation();
+            if (element == null)
+                return info;
+
+            var args = new AccessKeyPressedEventArgs(key);
+            element.RaiseEvent(args);
+            info.Target = args.Target;
+
+            return info;
+        }
+
+        /// <summary>
+        /// Checks if the focused element is a descendent of the owner.
+        /// </summary>
+        /// <param name="owner">The owner to check.</param>
+        /// <returns>If focused element is decendant of owner <c>true</c>, otherwise <c>false</c>. </returns>
+        private static bool IsFocusWithinOwner(IInputRoot owner)
+        {
+            var focusedElement = KeyboardDevice.Instance?.FocusedElement;
+            if (focusedElement is not InputElement inputElement)
+                return false;
+
+            var isAncestorOf = owner is Visual root && root.IsVisualAncestorOf(inputElement);
+            return isAncestorOf;
+        }
+
+        /// <summary>
+        /// Sorts the list of targets according to logical ancestors in the hierarchy
+        /// so that child elements, for example within in the content of a tab,
+        /// are processed before the next parent item i.e. the next tab item.
+        /// </summary>
+        private static List<IInputElement> SortByHierarchy(List<IInputElement> targets)
+        {
+            // bail out, if there are no targets to sort
+            if (targets.Count <= 1) 
+                return targets;
+            
+            var sorted = new List<IInputElement>(targets.Count);
+            var queue = new Queue<IInputElement>(targets);
+            while (queue.Count > 0)
+            {
+                var element = queue.Dequeue();
+            
+                // if the element was already added, do nothing
+                if (sorted.Contains(element))
+                    continue;
+            
+                // add the element itself
+                sorted.Add(element);
+
+                // if the element is not a potential parent, do nothing
+                if (element is not ILogical parentElement) 
+                    continue;
+                
+                // add all descendants of the element
+                sorted.AddRange(queue
+                    .Where(child => parentElement
+                        .IsLogicalAncestorOf(child as ILogical)));
+            }
+            return sorted;
+        }
+
+        private enum ProcessKeyResult
+        {
+            NoMatch,
+            MoreMatches,
+            LastMatch
+        }
+
+        private struct AccessKeyInformation
+        {
+            public IInputElement? Target { get; set; }
+        }
+    }
+
+    /// <summary>
+    /// The inputs to an AccessKeyPressedEventHandler
+    /// </summary>
+    internal class AccessKeyPressedEventArgs : RoutedEventArgs
+    {
+        /// <summary>
+        /// The constructor for AccessKeyPressed event args
+        /// </summary>
+        public AccessKeyPressedEventArgs()
+        {
+            RoutedEvent = AccessKeyHandler.AccessKeyPressedEvent;
+            Key = null;
+        }
+
+        /// <summary>
+        /// Constructor for AccessKeyPressed event args
+        /// </summary>
+        /// <param name="key"></param>
+        public AccessKeyPressedEventArgs(string key) : this()
+        {
+            RoutedEvent = AccessKeyHandler.AccessKeyPressedEvent;
+            Key = key;
+        }
+
+        /// <summary>
+        /// Target element for the element that raised this event.
+        /// </summary>
+        /// <value></value>
+        public IInputElement? Target { get; set; }
+
+        /// <summary>
+        /// Key that was pressed
+        /// </summary>
+        /// <value></value>
+        public string? Key { get; }
+    }
+
+    /// <summary>
+    /// Information pertaining to when the access key associated with an element is pressed
+    /// </summary>
+    internal class AccessKeyEventArgs : RoutedEventArgs
+    {
+        /// <summary>
+        /// Constructor
+        /// </summary>
+        internal AccessKeyEventArgs(string key, bool isMultiple)
+        {
+            RoutedEvent = AccessKeyHandler.AccessKeyEvent;
+
+            Key = key;
+            IsMultiple = isMultiple;
+        }
+
+        /// <summary>
+        /// The key that was pressed which invoked this access key
+        /// </summary>
+        /// <value></value>
+        public string Key { get; }
+
+        /// <summary>
+        /// Were there other elements which are also invoked by this key
+        /// </summary>
+        /// <value></value>
+        public bool IsMultiple { get; }
+    }
+
+    internal class AccessKeyRegistration
+    {
+        private readonly WeakReference<IInputElement> _target;
+        public string Key { get; }
+
+        public AccessKeyRegistration(string key, WeakReference<IInputElement> target)
+        {
+            _target = target;
+            Key = key;
+        }
+
+        public IInputElement? GetInputElement() =>
+            _target.TryGetTarget(out var target) ? target : null;
     }
 }
diff --git a/src/Avalonia.Base/Input/InputElement.cs b/src/Avalonia.Base/Input/InputElement.cs
index bdb65001687..5da27c68042 100644
--- a/src/Avalonia.Base/Input/InputElement.cs
+++ b/src/Avalonia.Base/Input/InputElement.cs
@@ -1,17 +1,15 @@
+#nullable enable
+
 using System;
 using System.Collections.Generic;
-using System.Linq;
 using Avalonia.Controls;
 using Avalonia.Controls.Metadata;
-using Avalonia.Data;
 using Avalonia.Input.GestureRecognizers;
 using Avalonia.Input.TextInput;
 using Avalonia.Interactivity;
 using Avalonia.Reactive;
 using Avalonia.VisualTree;
 
-#nullable enable
-
 namespace Avalonia.Input
 {
     /// <summary>
@@ -231,6 +229,10 @@ static InputElement()
             PointerPressedEvent.AddClassHandler<InputElement>((x, e) => x.OnGesturePointerPressed(e), handledEventsToo: true);
             PointerReleasedEvent.AddClassHandler<InputElement>((x, e) => x.OnGesturePointerReleased(e), handledEventsToo: true);
             PointerCaptureLostEvent.AddClassHandler<InputElement>((x, e) => x.OnGesturePointerCaptureLost(e), handledEventsToo: true);
+            
+            
+            // Access Key Handling
+            AccessKeyHandler.AccessKeyEvent.AddClassHandler<InputElement>((x, e) => x.OnAccessKey(e));
         }
 
         public InputElement()
@@ -282,7 +284,7 @@ public event EventHandler<TextInputEventArgs>? TextInput
             add { AddHandler(TextInputEvent, value); }
             remove { RemoveHandler(TextInputEvent, value); }
         }
-        
+
         /// <summary>
         /// Occurs when an input element gains input focus and input method is looking for the corresponding client
         /// </summary>
@@ -346,7 +348,7 @@ public event EventHandler<PointerCaptureLostEventArgs>? PointerCaptureLost
             add => AddHandler(PointerCaptureLostEvent, value);
             remove => RemoveHandler(PointerCaptureLostEvent, value);
         }
-        
+
         /// <summary>
         /// Occurs when the mouse is scrolled over the control.
         /// </summary>
@@ -355,7 +357,7 @@ public event EventHandler<PointerWheelEventArgs>? PointerWheelChanged
             add { AddHandler(PointerWheelChangedEvent, value); }
             remove { RemoveHandler(PointerWheelChangedEvent, value); }
         }
-        
+
         /// <summary>
         /// Occurs when a tap gesture occurs on the control.
         /// </summary>
@@ -364,7 +366,7 @@ public event EventHandler<TappedEventArgs>? Tapped
             add { AddHandler(TappedEvent, value); }
             remove { RemoveHandler(TappedEvent, value); }
         }
-        
+
         /// <summary>
         /// Occurs when a hold gesture occurs on the control.
         /// </summary>
@@ -409,7 +411,7 @@ public Cursor? Cursor
             get { return GetValue(CursorProperty); }
             set { SetValue(CursorProperty, value); }
         }
-        
+
         /// <summary>
         /// Gets a value indicating whether keyboard focus is anywhere within the element or its visual tree child elements.
         /// </summary>
@@ -515,6 +517,17 @@ protected override void OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventAr
             }
         }
 
+        /// <summary>
+        /// This method is used to execute the action on an effective IInputElement when a corresponding access key has been invoked.
+        /// By default, the Focus() method is invoked with the NavigationMethod.Tab to indicate a visual focus adorner.
+        /// Overwrite this method if other methods or additional functionality is needed when an item should receive the focus.
+        /// </summary>
+        /// <param name="e">AccessKeyEventArgs are passed on to indicate if there are multiple matches or not.</param>
+        protected virtual void OnAccessKey(RoutedEventArgs e)
+        {
+            Focus(NavigationMethod.Tab);
+        }
+
         /// <inheritdoc/>
         protected override void OnAttachedToVisualTreeCore(VisualTreeAttachmentEventArgs e)
         {
diff --git a/src/Avalonia.Controls/Button.cs b/src/Avalonia.Controls/Button.cs
index 614847ee4e8..9673eaadf75 100644
--- a/src/Avalonia.Controls/Button.cs
+++ b/src/Avalonia.Controls/Button.cs
@@ -104,7 +104,7 @@ public class Button : ContentControl, ICommandSource, IClickableControl
         static Button()
         {
             FocusableProperty.OverrideDefaultValue(typeof(Button), true);
-            AccessKeyHandler.AccessKeyPressedEvent.AddClassHandler<Button>((lbl, args) => lbl.OnAccessKey(args));
+            AccessKeyHandler.AccessKeyPressedEvent.AddClassHandler<Button>(OnAccessKeyPressed);
         }
 
         /// <summary>
@@ -199,7 +199,7 @@ public FlyoutBase? Flyout
 
         /// <inheritdoc/>
         protected override bool IsEnabledCore => base.IsEnabledCore && _commandCanExecute;
-
+        
         /// <inheritdoc/>
         protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
         {
@@ -278,7 +278,18 @@ protected override void OnDetachedFromLogicalTree(LogicalTreeAttachmentEventArgs
             }
         }
 
-        protected virtual void OnAccessKey(RoutedEventArgs e) => OnClick();
+        /// <inheritdoc />
+        protected override void OnAccessKey(RoutedEventArgs e)
+        {
+            if (e is AccessKeyEventArgs { IsMultiple: true })
+            {
+                base.OnAccessKey(e);
+            }
+            else
+            {
+                OnClick();    
+            }
+        }
 
         /// <inheritdoc/>
         protected override void OnKeyDown(KeyEventArgs e)
@@ -563,6 +574,14 @@ protected override void UpdateDataValidation(
 
         internal void PerformClick() => OnClick();
 
+        private static void OnAccessKeyPressed(Button sender, AccessKeyPressedEventArgs e)
+        {
+            if (e.Handled || e.Target is not null) 
+                return;
+            e.Target = sender;
+            e.Handled = true;
+        }
+        
         /// <summary>
         /// Called when the <see cref="ICommand.CanExecuteChanged"/> event fires.
         /// </summary>
diff --git a/src/Avalonia.Controls/Label.cs b/src/Avalonia.Controls/Label.cs
index 94ea66c4c1b..468c98ffce7 100644
--- a/src/Avalonia.Controls/Label.cs
+++ b/src/Avalonia.Controls/Label.cs
@@ -28,9 +28,8 @@ public IInputElement? Target
 
         static Label()
         {
-            AccessKeyHandler.AccessKeyPressedEvent.AddClassHandler<Label>((lbl, args) => lbl.LabelActivated(args));
-            // IsTabStopProperty.OverrideDefaultValue<Label>(false)
-            FocusableProperty.OverrideDefaultValue<Label>(false);
+            AccessKeyHandler.AccessKeyPressedEvent.AddClassHandler<Label>(OnAccessKeyPressed);
+            IsTabStopProperty.OverrideDefaultValue<Label>(false);
         }
 
         /// <summary>
@@ -40,13 +39,11 @@ public Label()
         {
         }
 
-        /// <summary>
-        /// Method which focuses <see cref="Target"/> input element
-        /// </summary>
-        private void LabelActivated(RoutedEventArgs args)
+
+        /// <inheritdoc />
+        protected override void OnAccessKey(RoutedEventArgs e)
         {
-            Target?.Focus();
-            args.Handled = Target != null;
+            LabelActivated(e);
         }
 
         /// <summary>
@@ -62,9 +59,25 @@ protected override void OnPointerPressed(PointerPressedEventArgs e)
             base.OnPointerPressed(e);
         }
 
+        /// <inheritdoc />
         protected override AutomationPeer OnCreateAutomationPeer()
         {
             return new LabelAutomationPeer(this);
         }
+
+        private void LabelActivated(RoutedEventArgs e)
+        {
+            Target?.Focus();
+            e.Handled = Target != null;
+        }
+
+        private static void OnAccessKeyPressed(Label label, AccessKeyPressedEventArgs e)
+        {
+            if (e is not { Handled: false, Target: null }) 
+                return;
+            
+            e.Target = label.Target;
+            e.Handled = true;
+        }
     }
 }
diff --git a/src/Avalonia.Controls/Menu.cs b/src/Avalonia.Controls/Menu.cs
index 6a8cf2b5150..56137287a0f 100644
--- a/src/Avalonia.Controls/Menu.cs
+++ b/src/Avalonia.Controls/Menu.cs
@@ -40,8 +40,9 @@ static Menu()
                 KeyboardNavigationMode.Once);
             AutomationProperties.AccessibilityViewProperty.OverrideDefaultValue<Menu>(AccessibilityView.Control);
             AutomationProperties.ControlTypeOverrideProperty.OverrideDefaultValue<Menu>(AutomationControlType.Menu);
+            AccessKeyHandler.AccessKeyPressedEvent.AddClassHandler<Menu>(OnAccessKeyPressed);
         }
-
+        
         /// <inheritdoc/>
         public override void Close()
         {
@@ -104,5 +105,14 @@ protected internal override void PrepareContainerForItemOverride(Control element
             if ((element as MenuItem)?.ItemContainerTheme == ItemContainerTheme)
                 element.ClearValue(ItemContainerThemeProperty);
         }
+        
+        private static void OnAccessKeyPressed(Menu sender, AccessKeyPressedEventArgs e)
+        {
+            if (e.Handled || e.Source is not StyledElement target) 
+                return;
+            
+            e.Target = DefaultMenuInteractionHandler.GetMenuItemCore(target);
+            e.Handled = true;
+        }
     }
 }
diff --git a/src/Avalonia.Controls/MenuItem.cs b/src/Avalonia.Controls/MenuItem.cs
index 13cd78f2fce..8cfc40bf0f2 100644
--- a/src/Avalonia.Controls/MenuItem.cs
+++ b/src/Avalonia.Controls/MenuItem.cs
@@ -143,6 +143,7 @@ static MenuItem()
             ClickEvent.AddClassHandler<MenuItem>((x, e) => x.OnClick(e));
             SubmenuOpenedEvent.AddClassHandler<MenuItem>((x, e) => x.OnSubmenuOpened(e));
             AutomationProperties.IsOffscreenBehaviorProperty.OverrideDefaultValue<MenuItem>(IsOffscreenBehavior.FromClip);
+            AccessKeyHandler.AccessKeyPressedEvent.AddClassHandler<MenuItem>(OnAccessKeyPressed);
         }
 
         public MenuItem()
@@ -565,7 +566,7 @@ protected override void UpdateDataValidation(
                 }
             }
         }
-
+        
         /// <summary>
         /// Closes all submenus of the menu item.
         /// </summary>
@@ -603,6 +604,15 @@ private static void CommandChanged(AvaloniaPropertyChangedEventArgs e)
             }
 
         }
+        
+        private static void OnAccessKeyPressed(MenuItem sender, AccessKeyPressedEventArgs e)
+        {
+            if (e is not { Handled: false, Target: null }) 
+                return;
+            
+            e.Target = sender;
+            e.Handled = true;
+        }
 
         /// <summary>
         /// Called when the <see cref="CommandParameter"/> property changes.
diff --git a/src/Avalonia.Controls/MenuItemAccessKeyHandler.cs b/src/Avalonia.Controls/MenuItemAccessKeyHandler.cs
index e1e7779f17c..5571773a9de 100644
--- a/src/Avalonia.Controls/MenuItemAccessKeyHandler.cs
+++ b/src/Avalonia.Controls/MenuItemAccessKeyHandler.cs
@@ -1,82 +1,17 @@
 using System;
-using System.Collections.Generic;
 using System.Linq;
 using Avalonia.Input;
-using Avalonia.Interactivity;
 
 namespace Avalonia.Controls
 {
     /// <summary>
     /// Handles access keys within a <see cref="MenuItem"/>
     /// </summary>
-    internal class MenuItemAccessKeyHandler : IAccessKeyHandler
+    internal class MenuItemAccessKeyHandler : AccessKeyHandler
     {
-        /// <summary>
-        /// The registered access keys.
-        /// </summary>
-        private readonly List<(string AccessKey, IInputElement Element)> _registered = new();
-
-        /// <summary>
-        /// The window to which the handler belongs.
-        /// </summary>
-        private IInputRoot? _owner;
-
-        /// <summary>
-        /// Gets or sets the window's main menu.
-        /// </summary>
-        /// <remarks>
-        /// This property is ignored as a menu item cannot have a main menu.
-        /// </remarks>
-        public IMainMenu? MainMenu { get; set; }
-
-        /// <summary>
-        /// Sets the owner of the access key handler.
-        /// </summary>
-        /// <param name="owner">The owner.</param>
-        /// <remarks>
-        /// This method can only be called once, typically by the owner itself on creation.
-        /// </remarks>
-        public void SetOwner(IInputRoot owner)
-        {
-            _ = owner ?? throw new ArgumentNullException(nameof(owner));
-
-            if (_owner != null)
-            {
-                throw new InvalidOperationException("AccessKeyHandler owner has already been set.");
-            }
-
-            _owner = owner;
-
-            _owner.AddHandler(InputElement.TextInputEvent, OnTextInput);
-        }
-
-        /// <summary>
-        /// Registers an input element to be associated with an access key.
-        /// </summary>
-        /// <param name="accessKey">The access key.</param>
-        /// <param name="element">The input element.</param>
-        public void Register(char accessKey, IInputElement element)
-        {
-            var existing = _registered.FirstOrDefault(x => x.Item2 == element);
-
-            if (existing != default)
-            {
-                _registered.Remove(existing);
-            }
-
-            _registered.Add((accessKey.ToString().ToUpperInvariant(), element));
-        }
-
-        /// <summary>
-        /// Unregisters the access keys associated with the input element.
-        /// </summary>
-        /// <param name="element">The input element.</param>
-        public void Unregister(IInputElement element)
+        protected override void OnSetOwner(IInputRoot owner)
         {
-            foreach (var i in _registered.Where(x => x.Item2 == element).ToList())
-            {
-                _registered.Remove(i);
-            }
+            owner.AddHandler(InputElement.TextInputEvent, OnTextInput);
         }
 
         /// <summary>
@@ -86,17 +21,17 @@ public void Unregister(IInputElement element)
         /// <param name="e">The event args.</param>
         protected virtual void OnTextInput(object? sender, TextInputEventArgs e)
         {
-            if (!string.IsNullOrWhiteSpace(e.Text))
-            {
-                var text = e.Text;
-                var focus = _registered
-                    .FirstOrDefault(x => string.Equals(x.AccessKey, text, StringComparison.OrdinalIgnoreCase)
-                        && x.Element.IsEffectivelyVisible).Element;
-
-                focus?.RaiseEvent(new RoutedEventArgs(AccessKeyHandler.AccessKeyPressedEvent));
-
-                e.Handled = true;
-            }
+            if (string.IsNullOrWhiteSpace(e.Text)) 
+                return;
+            
+            var key = e.Text;
+            var registration = Registrations
+                .FirstOrDefault(x => string.Equals(x.Key, key, StringComparison.OrdinalIgnoreCase));
+                
+            if (registration == null)
+                return;
+
+            e.Handled = ProcessKey(key, registration.GetInputElement());
         }
     }
 }
diff --git a/src/Avalonia.Controls/Platform/DefaultMenuInteractionHandler.cs b/src/Avalonia.Controls/Platform/DefaultMenuInteractionHandler.cs
index c4ee65ab133..8b699e2df7f 100644
--- a/src/Avalonia.Controls/Platform/DefaultMenuInteractionHandler.cs
+++ b/src/Avalonia.Controls/Platform/DefaultMenuInteractionHandler.cs
@@ -80,12 +80,16 @@ protected internal virtual void KeyDown(object? sender, KeyEventArgs e)
         protected internal virtual void AccessKeyPressed(object? sender, RoutedEventArgs e)
         {
             var item = GetMenuItemCore(e.Source as Control);
+            if (item is null)
+                return;
 
-            if (item == null)
+            if (e is AccessKeyEventArgs { IsMultiple: true })
             {
+                // in case we have multiple matches, only focus item and bail
+                item.Focus(NavigationMethod.Tab);
                 return;
             }
-
+            
             if (item.HasSubMenu && item.IsEffectivelyEnabled)
             {
                 Open(item, true);
@@ -290,7 +294,7 @@ internal void AttachCore(IMenu menu)
             Menu.KeyDown += KeyDown;
             Menu.PointerPressed += PointerPressed;
             Menu.PointerReleased += PointerReleased;
-            Menu.AddHandler(AccessKeyHandler.AccessKeyPressedEvent, AccessKeyPressed);
+            Menu.AddHandler(AccessKeyHandler.AccessKeyEvent, AccessKeyPressed);
             Menu.AddHandler(MenuBase.OpenedEvent, MenuOpened);
             Menu.AddHandler(MenuItem.PointerEnteredItemEvent, PointerEntered);
             Menu.AddHandler(MenuItem.PointerExitedItemEvent, PointerExited);
@@ -332,7 +336,7 @@ internal void DetachCore(IMenu menu)
             Menu.KeyDown -= KeyDown;
             Menu.PointerPressed -= PointerPressed;
             Menu.PointerReleased -= PointerReleased;
-            Menu.RemoveHandler(AccessKeyHandler.AccessKeyPressedEvent, AccessKeyPressed);
+            Menu.RemoveHandler(AccessKeyHandler.AccessKeyEvent, AccessKeyPressed);
             Menu.RemoveHandler(MenuBase.OpenedEvent, MenuOpened);
             Menu.RemoveHandler(MenuItem.PointerEnteredItemEvent, PointerEntered);
             Menu.RemoveHandler(MenuItem.PointerExitedItemEvent, PointerExited);
diff --git a/src/Avalonia.Controls/TabItem.cs b/src/Avalonia.Controls/TabItem.cs
index 0a0c3f5fc5c..1b2ab6505bc 100644
--- a/src/Avalonia.Controls/TabItem.cs
+++ b/src/Avalonia.Controls/TabItem.cs
@@ -40,7 +40,7 @@ static TabItem()
             DataContextProperty.Changed.AddClassHandler<TabItem>((x, e) => x.UpdateHeader(e));
             AutomationProperties.ControlTypeOverrideProperty.OverrideDefaultValue<TabItem>(AutomationControlType.TabItem);
             AutomationProperties.IsOffscreenBehaviorProperty.OverrideDefaultValue<TabItem>(IsOffscreenBehavior.FromClip);
-            AccessKeyHandler.AccessKeyPressedEvent.AddClassHandler<TabItem>((tabItem, args) => tabItem.TabItemActivated(args));
+            AccessKeyHandler.AccessKeyPressedEvent.AddClassHandler<TabItem>(OnAccessKeyPressed);
         }
 
         /// <summary>
@@ -61,13 +61,31 @@ public bool IsSelected
             set => SetValue(IsSelectedProperty, value);
         }
 
+        /// <inheritdoc />
+        protected override void OnAccessKey(RoutedEventArgs e)
+        {
+            Focus();
+            SetCurrentValue(IsSelectedProperty, true);
+            e.Handled = true;
+        }
+
         protected override AutomationPeer OnCreateAutomationPeer() => new ListItemAutomationPeer(this);
 
+        
         [Obsolete("Owner manages its children properties by itself")]
         protected void SubscribeToOwnerProperties(AvaloniaObject owner)
         {
         }
 
+        private static void OnAccessKeyPressed(TabItem tabItem, AccessKeyPressedEventArgs e)
+        {
+            if (e.Handled || (e.Target != null && tabItem.IsSelected)) 
+                return;
+            
+            e.Target = tabItem;
+            e.Handled = true;
+        }
+
         private void UpdateHeader(AvaloniaPropertyChangedEventArgs obj)
         {
             if (Header == null)
@@ -95,11 +113,5 @@ private void UpdateHeader(AvaloniaPropertyChangedEventArgs obj)
                 }
             }
         }
-
-        private void TabItemActivated(RoutedEventArgs args)
-        {
-            SetCurrentValue(IsSelectedProperty, true);
-            args.Handled = true;
-        }
     }
 }


Test patch:
diff --git a/tests/Avalonia.Base.UnitTests/Input/AccessKeyHandlerTests.cs b/tests/Avalonia.Base.UnitTests/Input/AccessKeyHandlerTests.cs
index ed1061e85a9..0fd1e318661 100644
--- a/tests/Avalonia.Base.UnitTests/Input/AccessKeyHandlerTests.cs
+++ b/tests/Avalonia.Base.UnitTests/Input/AccessKeyHandlerTests.cs
@@ -142,68 +142,85 @@ public void Should_Raise_Key_Events_For_Registered_Access_Key()
         }
 
         [Fact]
-        public void Should_Raise_AccessKeyPressed_For_Registered_Access_Key()
+        public void Should_Raise_AccessKey_For_Registered_Access_Key()
         {
-            var button = new Button();
-            var root = new TestRoot(button);
-            var target = new AccessKeyHandler();
-            var raised = 0;
+            using (UnitTestApplication.Start(TestServices.RealFocus))
+            {
+                var button = new Button();
+                var root = new TestRoot(button);
+                var target = new AccessKeyHandler();
+                var raised = 0;
 
-            target.SetOwner(root);
-            target.Register('A', button);
-            button.AddHandler(AccessKeyHandler.AccessKeyPressedEvent, (s, e) => ++raised);
+                KeyboardDevice.Instance?.SetFocusedElement(button, NavigationMethod.Unspecified, KeyModifiers.None);
 
-            KeyDown(root, Key.LeftAlt);
-            Assert.Equal(0, raised);
+                target.SetOwner(root);
+                target.Register('A', button);
+                button.AddHandler(AccessKeyHandler.AccessKeyEvent, (s, e) => ++raised);
 
-            KeyDown(root, Key.A, KeyModifiers.Alt);
-            Assert.Equal(1, raised);
+                KeyDown(root, Key.LeftAlt);
+                Assert.Equal(0, raised);
 
-            KeyUp(root, Key.A, KeyModifiers.Alt);
-            KeyUp(root, Key.LeftAlt);
+                KeyDown(root, Key.A, KeyModifiers.Alt);
+                Assert.Equal(1, raised);
 
-            Assert.Equal(1, raised);
+                KeyUp(root, Key.A, KeyModifiers.Alt);
+                KeyUp(root, Key.LeftAlt);
+
+                Assert.Equal(1, raised);
+            }
         }
 
-        [Fact]
-        public void Should_Not_Raise_AccessKeyPressed_For_Registered_Access_Key_When_Not_Effectively_Enabled()
+        [Theory]
+        [InlineData(false, 0)]
+        [InlineData(true, 1)]
+        public void Should_Raise_AccessKey_For_Registered_Access_Key_When_Effectively_Enabled(bool enabled, int expected)
         {
-            var button = new Button();
-            var root = new TestRoot(button) { IsEnabled = false };
-            var target = new AccessKeyHandler();
-            var raised = 0;
-
-            target.SetOwner(root);
-            target.Register('A', button);
-            button.AddHandler(AccessKeyHandler.AccessKeyPressedEvent, (s, e) => ++raised);
-
-            KeyDown(root, Key.LeftAlt);
-            Assert.Equal(0, raised);
-
-            KeyDown(root, Key.A, KeyModifiers.Alt);
-            Assert.Equal(0, raised);
-
-            KeyUp(root, Key.A, KeyModifiers.Alt);
-            KeyUp(root, Key.LeftAlt);
-
-            Assert.Equal(0, raised);
+            using (UnitTestApplication.Start(TestServices.RealFocus))
+            {
+                var button = new Button();
+                var root = new TestRoot(button) { IsEnabled = enabled };
+                var target = new AccessKeyHandler();
+                var raised = 0;
+                
+                KeyboardDevice.Instance?.SetFocusedElement(button, NavigationMethod.Unspecified, KeyModifiers.None);
+                
+                target.SetOwner(root);
+                target.Register('A', button);
+                button.AddHandler(AccessKeyHandler.AccessKeyEvent, (s, e) => ++raised);
+
+                KeyDown(root, Key.LeftAlt);
+                Assert.Equal(0, raised);
+
+                KeyDown(root, Key.A, KeyModifiers.Alt);
+                Assert.Equal(expected, raised);
+
+                KeyUp(root, Key.A, KeyModifiers.Alt);
+                KeyUp(root, Key.LeftAlt);
+                Assert.Equal(expected, raised);
+            }
         }
 
         [Fact]
         public void Should_Open_MainMenu_On_Alt_KeyUp()
         {
-            var root = new TestRoot();
-            var target = new AccessKeyHandler();
-            var menu = new Mock<IMainMenu>();
-
-            target.SetOwner(root);
-            target.MainMenu = menu.Object;
-
-            KeyDown(root, Key.LeftAlt);
-            menu.Verify(x => x.Open(), Times.Never);
-
-            KeyUp(root, Key.LeftAlt);
-            menu.Verify(x => x.Open(), Times.Once);
+            using (UnitTestApplication.Start(TestServices.RealFocus))
+            {
+                var target = new AccessKeyHandler();
+                var menu = new FakeMenu();
+                var root = new TestRoot(menu);
+
+                KeyboardDevice.Instance?.SetFocusedElement(menu, NavigationMethod.Unspecified,
+                    KeyModifiers.None);
+
+                target.SetOwner(root);
+                target.MainMenu = menu;
+
+                KeyDown(root, Key.LeftAlt);
+                Assert.Equal(0, menu.TimesOpenCalled);
+                
+                KeyUp(root, Key.LeftAlt);
+                Assert.Equal(1, menu.TimesOpenCalled);
+            }
         }
 
         private static void KeyDown(IInputElement target, Key key, KeyModifiers modifiers = KeyModifiers.None)
@@ -225,5 +242,15 @@ private static void KeyUp(IInputElement target, Key key, KeyModifiers modifiers
                 KeyModifiers = modifiers,
             });
         }
+        
+        class FakeMenu : Menu
+        {
+            public int TimesOpenCalled { get; set; }
+            
+            public override void Open()
+            {
+                TimesOpenCalled++;
+            }
+        }
     }
 }
diff --git a/tests/Avalonia.Base.UnitTests/Input/InputElement_Focus.cs b/tests/Avalonia.Base.UnitTests/Input/InputElement_Focus.cs
index 0bc3114665c..85d2be077f9 100644
--- a/tests/Avalonia.Base.UnitTests/Input/InputElement_Focus.cs
+++ b/tests/Avalonia.Base.UnitTests/Input/InputElement_Focus.cs
@@ -1,7 +1,5 @@
-using System;
 using Avalonia.Controls;
 using Avalonia.Input;
-using Avalonia.Interactivity;
 using Avalonia.UnitTests;
 using Xunit;
 
@@ -177,10 +175,10 @@ public void Focus_Should_Not_Get_Restored_To_Enabled_Control()
                 };
 
                 target.Focus();
-                target.RaiseEvent(new RoutedEventArgs(AccessKeyHandler.AccessKeyPressedEvent));
+                target.RaiseEvent(new AccessKeyEventArgs("b1", false));
                 Assert.False(target.IsEnabled);
                 Assert.False(target.IsFocused);
-                target1.RaiseEvent(new RoutedEventArgs(AccessKeyHandler.AccessKeyPressedEvent));
+                target1.RaiseEvent(new AccessKeyEventArgs("b2", false));
                 Assert.True(target.IsEnabled);
                 Assert.False(target.IsFocused);
             }
diff --git a/tests/Avalonia.Controls.UnitTests/ButtonTests.cs b/tests/Avalonia.Controls.UnitTests/ButtonTests.cs
index e3b974491aa..9d4a90a8124 100644
--- a/tests/Avalonia.Controls.UnitTests/ButtonTests.cs
+++ b/tests/Avalonia.Controls.UnitTests/ButtonTests.cs
@@ -293,9 +293,9 @@ public void Button_Invokes_CanExecute_When_CommandParameter_Changed()
             var raised = 0;
 
             target.Click += (s, e) => ++raised;
-
-            target.RaiseEvent(new RoutedEventArgs(AccessKeyHandler.AccessKeyPressedEvent));
-
+            
+            target.RaiseEvent(new AccessKeyEventArgs("b", false));
+            
             Assert.Equal(1, raised);
         }
 
@@ -304,7 +304,12 @@ public void Raises_Click_When_AccessKey_Raised()
         {
             var raised = 0;
             var ah = new AccessKeyHandler();
-            using var app = UnitTestApplication.Start(TestServices.StyledWindow.With(accessKeyHandler: ah));
+            var kd = new KeyboardDevice();
+            using var app = UnitTestApplication.Start(TestServices.StyledWindow
+                .With(
+                    accessKeyHandler: ah, 
+                    keyboardDevice: () => kd)
+            );
 
             var impl = CreateMockTopLevelImpl();
             var command = new TestCommand(p => p is bool value && value, _ => raised++);
@@ -329,6 +334,8 @@ public void Raises_Click_When_AccessKey_Raised()
                     })
                 },
             };
+            kd.SetFocusedElement(target, NavigationMethod.Unspecified, KeyModifiers.None);
+            
 
             root.ApplyTemplate();
             root.Presenter.UpdateChild();
@@ -349,7 +356,7 @@ public void Raises_Click_When_AccessKey_Raised()
             RaiseAccessKey(root, accessKey);
 
             Assert.Equal(1, raised);
-
+            
             static FuncControlTemplate<TestTopLevel> CreateTemplate()
             {
                 return new FuncControlTemplate<TestTopLevel>((x, scope) =>
@@ -409,7 +416,7 @@ public void Button_Invokes_Doesnt_Execute_When_Button_Disabled()
             target.IsEnabled = false;
             target.Click += (s, e) => ++raised;
 
-            target.RaiseEvent(new RoutedEventArgs(AccessKeyHandler.AccessKeyPressedEvent));
+            target.RaiseEvent(new AccessKeyEventArgs("b", false));
 
             Assert.Equal(0, raised);
         }
diff --git a/tests/Avalonia.Controls.UnitTests/TabControlTests.cs b/tests/Avalonia.Controls.UnitTests/TabControlTests.cs
index 4189ecd9de0..a4cc2b4f6b0 100644
--- a/tests/Avalonia.Controls.UnitTests/TabControlTests.cs
+++ b/tests/Avalonia.Controls.UnitTests/TabControlTests.cs
@@ -724,7 +724,12 @@ public void TabItem_TabStripPlacement_Should_Be_Correctly_Set_For_New_Items()
         public void Should_TabControl_Recognizes_AccessKey(Key accessKey, int selectedTabIndex)
         {
             var ah = new AccessKeyHandler();
-            using (UnitTestApplication.Start(TestServices.StyledWindow.With(accessKeyHandler: ah)))
+            var kd = new KeyboardDevice();
+            using (UnitTestApplication.Start(TestServices.StyledWindow
+                       .With(
+                           accessKeyHandler: ah,
+                           keyboardDevice: () => kd)
+                   ))
             {
                 var impl = CreateMockTopLevelImpl();
 
@@ -742,7 +747,8 @@ public void Should_TabControl_Recognizes_AccessKey(Key accessKey, int selectedTa
                         new TabItem { Header = "_Disabled", IsEnabled = false },
                     }
                 };
-
+                kd.SetFocusedElement((TabItem)tabControl.Items[selectedTabIndex], NavigationMethod.Unspecified, KeyModifiers.None);
+                
                 var root = new TestTopLevel(impl.Object)
                 {
                     Template = CreateTemplate(),
diff --git a/tests/Avalonia.Markup.UnitTests/Data/BindingTests_Method.cs b/tests/Avalonia.Markup.UnitTests/Data/BindingTests_Method.cs
index e613a178d5a..4b2c2494af6 100644
--- a/tests/Avalonia.Markup.UnitTests/Data/BindingTests_Method.cs
+++ b/tests/Avalonia.Markup.UnitTests/Data/BindingTests_Method.cs
@@ -17,7 +17,7 @@ public void Binding_To_Private_Methods_Shouldnt_Work()
                 DataContext = vm,
                 [!Button.CommandProperty] = new Binding("MyMethod"),
             };
-            target.RaiseEvent(new RoutedEventArgs(AccessKeyHandler.AccessKeyPressedEvent));
+            target.RaiseEvent(new AccessKeyEventArgs("b", false));
 
             Assert.False(vm.IsSet);
         }