Skip to content

Commit

Permalink
Check if physical ctrlKey is pressed
Browse files Browse the repository at this point in the history
  • Loading branch information
moffatman committed Mar 17, 2023
1 parent 9e2cb62 commit b39ef2e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 24 deletions.
11 changes: 5 additions & 6 deletions lib/web_ui/lib/src/engine/keyboard_binding.dart
Expand Up @@ -35,8 +35,8 @@ final int _kLogicalMetaRight = kWebLogicalLocationMap['Meta']![_kLocationRight]!

final int _kPhysicalAltLeft = kWebToPhysicalKey['AltLeft']!;
final int _kPhysicalAltRight = kWebToPhysicalKey['AltRight']!;
final int _kPhysicalControlLeft = kWebToPhysicalKey['ControlLeft']!;
final int _kPhysicalControlRight = kWebToPhysicalKey['ControlRight']!;
final int kPhysicalControlLeft = kWebToPhysicalKey['ControlLeft']!;
final int kPhysicalControlRight = kWebToPhysicalKey['ControlRight']!;
final int _kPhysicalShiftLeft = kWebToPhysicalKey['ShiftLeft']!;
final int _kPhysicalShiftRight = kWebToPhysicalKey['ShiftRight']!;
final int _kPhysicalMetaLeft = kWebToPhysicalKey['MetaLeft']!;
Expand Down Expand Up @@ -615,8 +615,8 @@ class KeyboardConverter {
eventTimestamp,
);
_synthesizeModifierIfNeeded(
_kPhysicalControlLeft,
_kPhysicalControlRight,
kPhysicalControlLeft,
kPhysicalControlRight,
_kLogicalControlLeft,
controlPressed ? ui.KeyEventType.down : ui.KeyEventType.up,
eventTimestamp,
Expand Down Expand Up @@ -694,8 +694,7 @@ class KeyboardConverter {
_pressingRecords.remove(physical);
}

@visibleForTesting
bool debugKeyIsPressed(int physical) {
bool keyIsPressed(int physical) {
return _pressingRecords.containsKey(physical);
}
}
7 changes: 6 additions & 1 deletion lib/web_ui/lib/src/engine/pointer_binding.dart
Expand Up @@ -456,7 +456,12 @@ mixin _WheelEventListenerMixin on _BaseAdapter {

final List<ui.PointerData> data = <ui.PointerData>[];
final ui.Offset offset = computeEventOffsetToTarget(event, glassPaneElement);
if (event.ctrlKey) {
bool ignoreCtrlKey = false;
if (operatingSystem == OperatingSystem.macOs) {
ignoreCtrlKey = (KeyboardBinding.instance?.converter.keyIsPressed(kPhysicalControlLeft) ?? false) ||
(KeyboardBinding.instance?.converter.keyIsPressed(kPhysicalControlLeft) ?? false);
}
if (event.ctrlKey && !ignoreCtrlKey) {
_pointerDataConverter.convert(
data,
change: ui.PointerChange.hover,
Expand Down
66 changes: 49 additions & 17 deletions lib/web_ui/test/engine/pointer_binding_test.dart
Expand Up @@ -544,8 +544,8 @@ void testMain() {
final KeyboardConverter keyboardConverter = createKeyboardConverter(keyDataList);
PointerBinding.instance!.debugOverrideKeyboardConverter(keyboardConverter);

expect(keyboardConverter.debugKeyIsPressed(physicalLeft), false);
expect(keyboardConverter.debugKeyIsPressed(physicalRight), false);
expect(keyboardConverter.keyIsPressed(physicalLeft), false);
expect(keyboardConverter.keyIsPressed(physicalRight), false);
glassPane.dispatchEvent(context.primaryDown());
expect(keyDataList.length, 1);
expectKeyData(keyDataList.last,
Expand Down Expand Up @@ -594,8 +594,8 @@ void testMain() {
PointerBinding.instance!.debugOverrideKeyboardConverter(keyboardConverter);

keyboardConverter.handleEvent(keyDownEvent('${key}Left', key, modifiers, kLocationLeft));
expect(keyboardConverter.debugKeyIsPressed(physicalLeft), true);
expect(keyboardConverter.debugKeyIsPressed(physicalRight), false);
expect(keyboardConverter.keyIsPressed(physicalLeft), true);
expect(keyboardConverter.keyIsPressed(physicalRight), false);
keyDataList.clear(); // Remove key data generated by handleEvent

glassPane.dispatchEvent(context.primaryDown());
Expand All @@ -614,8 +614,8 @@ void testMain() {
PointerBinding.instance!.debugOverrideKeyboardConverter(keyboardConverter);

keyboardConverter.handleEvent(keyDownEvent('${key}Right', key, modifiers, kLocationRight));
expect(keyboardConverter.debugKeyIsPressed(physicalLeft), false);
expect(keyboardConverter.debugKeyIsPressed(physicalRight), true);
expect(keyboardConverter.keyIsPressed(physicalLeft), false);
expect(keyboardConverter.keyIsPressed(physicalRight), true);
keyDataList.clear(); // Remove key data generated by handleEvent

glassPane.dispatchEvent(context.primaryDown());
Expand Down Expand Up @@ -664,8 +664,8 @@ void testMain() {
PointerBinding.instance!.debugOverrideKeyboardConverter(keyboardConverter);

keyboardConverter.handleEvent(keyDownEvent('${key}Left', key, modifiers, kLocationLeft));
expect(keyboardConverter.debugKeyIsPressed(physicalLeft), true);
expect(keyboardConverter.debugKeyIsPressed(physicalRight), false);
expect(keyboardConverter.keyIsPressed(physicalLeft), true);
expect(keyboardConverter.keyIsPressed(physicalRight), false);
keyDataList.clear(); // Remove key data generated by handleEvent

glassPane.dispatchEvent(context.primaryDown());
Expand All @@ -677,7 +677,7 @@ void testMain() {
character: null,
synthesized: true,
);
expect(keyboardConverter.debugKeyIsPressed(physicalLeft), false);
expect(keyboardConverter.keyIsPressed(physicalLeft), false);
}

// Should synthesize a modifier right key up event when DOM event indicates
Expand All @@ -693,8 +693,8 @@ void testMain() {
PointerBinding.instance!.debugOverrideKeyboardConverter(keyboardConverter);

keyboardConverter.handleEvent(keyDownEvent('${key}Right', key, modifiers, kLocationRight));
expect(keyboardConverter.debugKeyIsPressed(physicalLeft), false);
expect(keyboardConverter.debugKeyIsPressed(physicalRight), true);
expect(keyboardConverter.keyIsPressed(physicalLeft), false);
expect(keyboardConverter.keyIsPressed(physicalRight), true);
keyDataList.clear(); // Remove key data generated by handleEvent

glassPane.dispatchEvent(context.primaryDown());
Expand All @@ -706,7 +706,7 @@ void testMain() {
character: null,
synthesized: true,
);
expect(keyboardConverter.debugKeyIsPressed(physicalRight), false);
expect(keyboardConverter.keyIsPressed(physicalRight), false);
}

context.altPressed = false;
Expand Down Expand Up @@ -745,8 +745,8 @@ void testMain() {
final KeyboardConverter keyboardConverter = createKeyboardConverter(keyDataList);
PointerBinding.instance!.debugOverrideKeyboardConverter(keyboardConverter);

expect(keyboardConverter.debugKeyIsPressed(physicalLeft), false);
expect(keyboardConverter.debugKeyIsPressed(physicalRight), false);
expect(keyboardConverter.keyIsPressed(physicalLeft), false);
expect(keyboardConverter.keyIsPressed(physicalRight), false);
keyDataList.clear(); // Remove key data generated by handleEvent

glassPane.dispatchEvent(context.primaryDown());
Expand Down Expand Up @@ -783,7 +783,7 @@ void testMain() {

// Simulate pressing `AltGr` key.
keyboardConverter.handleEvent(keyDownEvent('AltRight', 'AltGraph'));
expect(keyboardConverter.debugKeyIsPressed(physicalAltRight), true);
expect(keyboardConverter.keyIsPressed(physicalAltRight), true);
keyDataList.clear(); // Remove key data generated by handleEvent.

glassPane.dispatchEvent(context.primaryDown());
Expand All @@ -795,7 +795,7 @@ void testMain() {
character: null,
synthesized: true,
);
expect(keyboardConverter.debugKeyIsPressed(physicalAltRight), false);
expect(keyboardConverter.keyIsPressed(physicalAltRight), false);
},
);

Expand Down Expand Up @@ -1428,7 +1428,21 @@ void testMain() {
ctrlKey: true,
));

expect(packets, hasLength(2));
debugOperatingSystemOverride = OperatingSystem.macOs;
KeyboardBinding.instance?.converter.handleEvent(keyDownEvent('ControlLeft', 'Control', kCtrl));

glassPane.dispatchEvent(context.wheel(
buttons: 0,
clientX: 10,
clientY: 10,
deltaX: 0,
deltaY: 240,
ctrlKey: true,
));

KeyboardBinding.instance?.converter.handleEvent(keyUpEvent('ControlLeft', 'Control', kCtrl));

expect(packets, hasLength(3));

// An add will be synthesized.
expect(packets[0].data, hasLength(2));
Expand Down Expand Up @@ -1468,6 +1482,24 @@ void testMain() {
expect(packets[1].data[0].physicalDeltaX, equals(0.0));
expect(packets[1].data[0].physicalDeltaY, equals(0.0));
expect(packets[1].data[0].scale, closeTo(0.60653065971, 1e-10)); // math.exp(-100/200)

// [macOS only]: Because ctrlKey is true, but the key is pressed physically, it will be a scroll.
expect(packets[2].data, hasLength(1));
expect(packets[2].data[0].change, equals(ui.PointerChange.hover));
expect(
packets[2].data[0].signalKind, equals(ui.PointerSignalKind.scroll));
expect(
packets[2].data[0].kind, equals(ui.PointerDeviceKind.mouse));
expect(packets[2].data[0].pointerIdentifier, equals(0));
expect(packets[2].data[0].synthesized, isFalse);
expect(packets[2].data[0].physicalX, equals(10.0 * dpi));
expect(packets[2].data[0].physicalY, equals(10.0 * dpi));
expect(packets[2].data[0].physicalDeltaX, equals(0.0));
expect(packets[2].data[0].physicalDeltaY, equals(0.0));
expect(packets[2].data[0].scrollDeltaX, equals(0.0));
expect(packets[2].data[0].scrollDeltaY, equals(240.0));

debugOperatingSystemOverride = null;
},
);

Expand Down

0 comments on commit b39ef2e

Please sign in to comment.