Skip to content

Flutter does not receive key up events when cmd key is pressed (macOS) #38076

Closed

Description

@shyndman

This has the effect of "sticking" keys in RawKeyboard.instance.keysPressed, which in turn causes the Shortcuts widget to fail.

After a bit of research, it might be worthwhile to send events directly from the NSApplication to the engine. Looks like everything passes through - sendEvent before it is interpreted and delegated to the various the NSResponder methods.

Related to #36001

Activity

  1. shyndman commented on Aug 10, 2019

    @shyndman
    ContributorAuthor
  2. added
    engineflutter/engine related. See also e: labels.
    platform-macosBuilding on or for macOS specifically
    on Aug 12, 2019
  3. shyndman commented on Aug 12, 2019

    @shyndman
    ContributorAuthor

    Any thoughts on approach Stuart? I had the thought that subclassing NSApplication might make integration into existing projects more difficult, but that could be outweighed by the value of dealing with the raw event stream.

    Also, is the preference to do this work in Objective-C?

  4. stuartmorgan-g commented on Aug 12, 2019

    @stuartmorgan-g
    Contributor

    Any thoughts on approach Stuart?

    No, I haven't spent any time investigating this issue. Labelling an issue isn't an indication that I've spent time investigating it.

    Also, is the preference to do this work in Objective-C?

    Well, "this work" hasn't been clearly defined; the answer to that question depends on whether the work in in the framework or the template.

  5. added this to the milestone on Aug 13, 2019
  6. shyndman commented on Aug 19, 2019

    @shyndman
    ContributorAuthor

    Just a quick update here. We managed to get around this with my suggestion above. I'm not sure if it's the best way to go about it, but might be useful when you choose to implement.

    @objc(MyApplication)
    class MyApplication : NSApplication {
      override func sendEvent(_ event: NSEvent) {
        if event.type == .keyUp && event.modifierFlags.intersection(.command).rawValue != 0 {
          self.keyWindow?.sendEvent(event)
        } else {
          super.sendEvent(event)
        }
      }
    }

    Before going down this path, we first tried hooking into ExampleWindow's sendEvent (which seems less intrusive), but the keyUp never arrives.

  7. hpoul commented on Aug 30, 2019

    @hpoul
    Contributor

    I'm experiencing a similar issue of "sticky" keys in RawKeyboard.instance.keysPressed
    when I have a key down for shift+X but then let go of the shift before releasing x i'll have a sticky X which never disappears and hence the Shortcuts widget & co won't work at all.
    Should I open a separate issue for this? Imho the sticky key problem seems very related, especially i'm not sure how this could work in a desktop environment where it's not unusual to switch apps or otherwise lose focus before receiving a matching keyup event?

        RawKeyboard.instance.addListener(_debugEvent);
    
      void _debugEvent(RawKeyEvent ev) {
        final keys = RawKeyboard.instance.keysPressed.map((k) => '${k.keyId} (${k.keyLabel})');
        _logger.fine('Got key event. $keys');
      }
    Got key event. (65 (A))           //// keep shift + A pressed
    Got key event. (65 (A))
    Got key event. (65 (A), 97 (a))   /// released shift
    Got key event. (65 (A), 97 (a))
    Got key event. (65 (A))           /// released A
    
  8. shyndman commented on Sep 4, 2019

    @shyndman
    ContributorAuthor

    Hi @hpoul,

    This narrows down some problems we've been seeing, so thanks. That said, it's different. It would make sense to file a separate issue.

  9. gspencergoog commented on Oct 18, 2019

    @gspencergoog
    Contributor

    /cc @franciscojma86

    This is a result of macOS engine not monitoring the flagsChanged message. We'll need to keep track of the modifier state and send additional key events when modifiers are pressed and released, similar to this example for CapsLock: https://developer.apple.com/library/archive/qa/qa1519/_index.html

  10. franciscojma86 commented on Oct 23, 2019

    @franciscojma86
    Contributor

    Landed, @shyndman LMK if you see any issues, thanks for filing!

  11. github-actions commented on Aug 13, 2021

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  12. locked as resolved and limited conversation to collaborators on Aug 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

a: desktopRunning on desktopengineflutter/engine related. See also e: labels.platform-macosBuilding on or for macOS specifically

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions