diff --git a/app/lib/core/keyboard/key_modifier_sync.dart b/app/lib/core/keyboard/key_modifier_sync.dart index 60c5cf0e..5291f1bf 100644 --- a/app/lib/core/keyboard/key_modifier_sync.dart +++ b/app/lib/core/keyboard/key_modifier_sync.dart @@ -126,6 +126,14 @@ class KeyModifierSync { _released[key] = logical; _lift(key, logical, data.timeStamp); changed = true; + } else if (!modifier.flag(flags)) { + // The event says this modifier is up, yet the framework still + // holds it: its release was lost (a chord ended while the page + // moved, a key event the engine never saw). Left held, it turns + // the next Alt+K into Ctrl+Alt+K and no shortcut matches. The + // event's own flags are the truth, so release it for good. + final logical = _keyboard.lookUpLayout(key); + if (logical != null) _lift(key, logical, data.timeStamp); } } } diff --git a/app/test/core/keyboard/key_modifier_sync_test.dart b/app/test/core/keyboard/key_modifier_sync_test.dart index 62a54612..f242b263 100644 --- a/app/test/core/keyboard/key_modifier_sync_test.dart +++ b/app/test/core/keyboard/key_modifier_sync_test.dart @@ -207,6 +207,36 @@ void main() { }); }); + test('a real Ctrl whose release was lost is let go by the next key ' + 'that says Ctrl is up, so Alt+K stays Alt+K', () { + final h = SyncHarness(); + final ctrl = ( + PhysicalKeyboardKey.controlLeft, + LogicalKeyboardKey.controlLeft, + ); + final alt = (PhysicalKeyboardKey.altLeft, LogicalKeyboardKey.altLeft); + final k = (PhysicalKeyboardKey.keyK, LogicalKeyboardKey.keyK); + // Ctrl+A, then Ctrl's release never reaches the framework. + h.send(_key(_down, ctrl), _ctrl); + h.send( + _key(_down, (PhysicalKeyboardKey.keyA, LogicalKeyboardKey.keyA)), + _ctrl, + ); + h.send( + _key(_up, (PhysicalKeyboardKey.keyA, LogicalKeyboardKey.keyA)), + _ctrl, + ); + expect(h.pressed.keys, contains(PhysicalKeyboardKey.controlLeft)); + // Alt+K: the event's flags hold Alt only. + const altOnly = KeyEventModifiers(alt: true); + h.send(_key(_down, alt), altOnly); + h.send(_key(_down, k), altOnly); + expect(h.pressed.keys, { + PhysicalKeyboardKey.altLeft, + PhysicalKeyboardKey.keyK, + }); + }); + test('unknown flags leave the event alone', () { final h = SyncHarness(); h.send(_key(_down, _c));