Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⌘+⌥+<any key> input is not handled on macOS (Cmd+Option) (Meta+Alt) #5976

Closed
Mikolaytis opened this issue May 24, 2021 · 2 comments · Fixed by #6491
Closed

⌘+⌥+<any key> input is not handled on macOS (Cmd+Option) (Meta+Alt) #5976

Mikolaytis opened this issue May 24, 2021 · 2 comments · Fixed by #6491
Assignees
Labels

Comments

@Mikolaytis
Copy link
Contributor

Mikolaytis commented May 24, 2021

Describe the bug
On macOS cmd+option+ input events are not working for the first char press. only for a second press, if you are not lifted up cmd+option. Other native apps handling shortcuts without any issues - so this is not an OS configuration fail.

To Reproduce
I've got a repro attached to the InputManager.PreProcess event so this is not a code/markup fail by me.

    public partial class MainWindow : Window
    {
        public static MainWindow Self;
        public MainWindow()
        {
            AvaloniaXamlLoader.Load(this);
            Self = this;
            AvaloniaLocator.Current.GetService<IInputManager>().PreProcess.Subscribe(new InputObserverRaw());
        }
        private class InputObserverRaw : IObserver<RawInputEventArgs>
        {
            public void OnCompleted() => Self.Title += "Completed";

            public void OnError(Exception error) => Self.Title += error.ToString();

            public void OnNext(RawInputEventArgs value)
            {
                if (value is RawKeyEventArgs args)
                {
                    Self.Title = args.Type + ": " + args.Modifiers + " => " + args.Key;
                }
            }
        }
        private int _count;
        protected override void OnKeyDown(KeyEventArgs e)
        {
            if (e.Key == Key.K && e.KeyModifiers == (KeyModifiers.Alt | KeyModifiers.Meta))
            {
                _count++;
                Title = "PRESSED! Meta+Alt+K " + _count;
                e.Handled = true;
            }
            base.OnKeyDown(e);
        }
    }
  1. Copy content above
  2. Create new project from Avalonia template
  3. Paste content to the MainWindow.axaml
  4. Launch App
  5. Hold cmd -> option -> Press K one time - no working, press again - working (window title showing the result)
  6. release all keys and try again step 5.
  7. Hold option -> cmd -> Press K - this order works!

Expected behavior
Hold cmd -> option -> input handling works on MacOS on first press

Desktop (please complete the following information):

  • OS: MacOS Big Sur
  • Version 11.3 Beta (Also our QA getting same issues on previous non-beta versions)
@Mikolaytis
Copy link
Contributor Author

I've debugged into result: source of a problem is AccessKeyHandler. It's setting Handled = true here:

protected virtual void OnPreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.LeftAlt || e.Key == Key.RightAlt)
            {
                _altIsDown = true;

                ...
                // We always handle the Alt key.
                e.Handled = true;
            }
            else if (_altIsDown)
            {
                _ignoreAltUp = true;
            }
        }

Comment says - we always handle alt key...
I will look into this a bit more deep later and maybe will come with a fix.

@Mikolaytis
Copy link
Contributor Author

I think this is wrong. AccessKeyHandler should not eat up all key alt downs. This is causing some issues in our app.

  1. we cannot register alt down
  2. hotkeys with alt do not work for the first press
    So I will provide a fix that moves e.handles = true into the else...
    Now it will eat alt presses only if main menu is open.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants