|
2 | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | 3 | // found in the LICENSE file. |
4 | 4 |
|
| 5 | +import 'package:meta/meta.dart'; |
5 | 6 | import 'package:ui/ui.dart' as ui; |
6 | 7 |
|
7 | 8 | import '../engine.dart' show registerHotRestartListener; |
@@ -116,6 +117,7 @@ class KeyboardBinding { |
116 | 117 | } |
117 | 118 | } |
118 | 119 |
|
| 120 | + KeyboardConverter get converter => _converter; |
119 | 121 | late final KeyboardConverter _converter; |
120 | 122 | final Map<String, DomEventListener> _listeners = <String, DomEventListener>{}; |
121 | 123 |
|
@@ -162,104 +164,6 @@ class KeyboardBinding { |
162 | 164 | _converter = KeyboardConverter(_onKeyData, onMacOs: operatingSystem == OperatingSystem.macOs); |
163 | 165 | } |
164 | 166 |
|
165 | | - // Synthesize modifier keys up or down events only when the known pressing states are different. |
166 | | - void synthesizeModifiersIfNeeded( |
167 | | - bool altPressed, |
168 | | - bool controlPressed, |
169 | | - bool metaPressed, |
170 | | - bool shiftPressed, |
171 | | - num eventTimestamp, |
172 | | - ) { |
173 | | - _synthesizeModifierIfNeeded( |
174 | | - _kPhysicalAltLeft, |
175 | | - _kPhysicalAltRight, |
176 | | - _kLogicalAltLeft, |
177 | | - _kLogicalAltRight, |
178 | | - altPressed ? ui.KeyEventType.down : ui.KeyEventType.up, |
179 | | - eventTimestamp, |
180 | | - ); |
181 | | - _synthesizeModifierIfNeeded( |
182 | | - _kPhysicalControlLeft, |
183 | | - _kPhysicalControlRight, |
184 | | - _kLogicalControlLeft, |
185 | | - _kLogicalControlRight, |
186 | | - controlPressed ? ui.KeyEventType.down : ui.KeyEventType.up, |
187 | | - eventTimestamp, |
188 | | - ); |
189 | | - _synthesizeModifierIfNeeded( |
190 | | - _kPhysicalMetaLeft, |
191 | | - _kPhysicalMetaRight, |
192 | | - _kLogicalMetaLeft, |
193 | | - _kLogicalMetaRight, |
194 | | - metaPressed ? ui.KeyEventType.down : ui.KeyEventType.up, |
195 | | - eventTimestamp, |
196 | | - ); |
197 | | - _synthesizeModifierIfNeeded( |
198 | | - _kPhysicalShiftLeft, |
199 | | - _kPhysicalShiftRight, |
200 | | - _kLogicalShiftLeft, |
201 | | - _kLogicalShiftRight, |
202 | | - shiftPressed ? ui.KeyEventType.down : ui.KeyEventType.up, |
203 | | - eventTimestamp, |
204 | | - ); |
205 | | - } |
206 | | - |
207 | | - void _synthesizeModifierIfNeeded( |
208 | | - int physicalLeft, |
209 | | - int physicalRight, |
210 | | - int logicalLeft, |
211 | | - int logicalRight, |
212 | | - ui.KeyEventType type, |
213 | | - num domTimestamp, |
214 | | - ) { |
215 | | - final bool leftPressed = _converter._pressingRecords.containsKey(physicalLeft); |
216 | | - final bool rightPressed = _converter._pressingRecords.containsKey(physicalRight); |
217 | | - final bool alreadyPressed = leftPressed || rightPressed; |
218 | | - final bool synthesizeDown = type == ui.KeyEventType.down && !alreadyPressed; |
219 | | - final bool synthesizeUp = type == ui.KeyEventType.up && alreadyPressed; |
220 | | - |
221 | | - // Synthesize a down event only for the left key if right and left are not pressed |
222 | | - if (synthesizeDown) { |
223 | | - _synthesizeKeyDownEvent(domTimestamp, physicalLeft, logicalLeft); |
224 | | - } |
225 | | - |
226 | | - // Synthesize an up event for left key if pressed |
227 | | - if (synthesizeUp && leftPressed) { |
228 | | - _synthesizeKeyUpEvent(domTimestamp, physicalLeft, logicalLeft); |
229 | | - } |
230 | | - |
231 | | - // Synthesize an up event for right key if pressed |
232 | | - if (synthesizeUp && rightPressed) { |
233 | | - _synthesizeKeyUpEvent(domTimestamp, physicalRight, logicalRight); |
234 | | - } |
235 | | - } |
236 | | - |
237 | | - void _synthesizeKeyDownEvent(num domTimestamp, int physical, int logical) { |
238 | | - _converter.performDispatchKeyData(ui.KeyData( |
239 | | - timeStamp: _eventTimeStampToDuration(domTimestamp), |
240 | | - type: ui.KeyEventType.down, |
241 | | - physical: physical, |
242 | | - logical: logical, |
243 | | - character: null, |
244 | | - synthesized: true, |
245 | | - )); |
246 | | - // Update pressing state |
247 | | - _converter._pressingRecords[physical] = logical; |
248 | | - } |
249 | | - |
250 | | - void _synthesizeKeyUpEvent(num domTimestamp, int physical, int logical) { |
251 | | - _converter.performDispatchKeyData(ui.KeyData( |
252 | | - timeStamp: _eventTimeStampToDuration(domTimestamp), |
253 | | - type: ui.KeyEventType.up, |
254 | | - physical: physical, |
255 | | - logical: logical, |
256 | | - character: null, |
257 | | - synthesized: true, |
258 | | - )); |
259 | | - // Update pressing states |
260 | | - _converter._pressingRecords.remove(physical); |
261 | | - } |
262 | | - |
263 | 167 | void _reset() { |
264 | 168 | _clearListeners(); |
265 | 169 | _converter.dispose(); |
@@ -667,4 +571,107 @@ class KeyboardConverter { |
667 | 571 | _dispatchKeyData = null; |
668 | 572 | } |
669 | 573 | } |
| 574 | + |
| 575 | + // Synthesize modifier keys up or down events only when the known pressing states are different. |
| 576 | + void synthesizeModifiersIfNeeded( |
| 577 | + bool altPressed, |
| 578 | + bool controlPressed, |
| 579 | + bool metaPressed, |
| 580 | + bool shiftPressed, |
| 581 | + num eventTimestamp, |
| 582 | + ) { |
| 583 | + _synthesizeModifierIfNeeded( |
| 584 | + _kPhysicalAltLeft, |
| 585 | + _kPhysicalAltRight, |
| 586 | + _kLogicalAltLeft, |
| 587 | + _kLogicalAltRight, |
| 588 | + altPressed ? ui.KeyEventType.down : ui.KeyEventType.up, |
| 589 | + eventTimestamp, |
| 590 | + ); |
| 591 | + _synthesizeModifierIfNeeded( |
| 592 | + _kPhysicalControlLeft, |
| 593 | + _kPhysicalControlRight, |
| 594 | + _kLogicalControlLeft, |
| 595 | + _kLogicalControlRight, |
| 596 | + controlPressed ? ui.KeyEventType.down : ui.KeyEventType.up, |
| 597 | + eventTimestamp, |
| 598 | + ); |
| 599 | + _synthesizeModifierIfNeeded( |
| 600 | + _kPhysicalMetaLeft, |
| 601 | + _kPhysicalMetaRight, |
| 602 | + _kLogicalMetaLeft, |
| 603 | + _kLogicalMetaRight, |
| 604 | + metaPressed ? ui.KeyEventType.down : ui.KeyEventType.up, |
| 605 | + eventTimestamp, |
| 606 | + ); |
| 607 | + _synthesizeModifierIfNeeded( |
| 608 | + _kPhysicalShiftLeft, |
| 609 | + _kPhysicalShiftRight, |
| 610 | + _kLogicalShiftLeft, |
| 611 | + _kLogicalShiftRight, |
| 612 | + shiftPressed ? ui.KeyEventType.down : ui.KeyEventType.up, |
| 613 | + eventTimestamp, |
| 614 | + ); |
| 615 | + } |
| 616 | + |
| 617 | + void _synthesizeModifierIfNeeded( |
| 618 | + int physicalLeft, |
| 619 | + int physicalRight, |
| 620 | + int logicalLeft, |
| 621 | + int logicalRight, |
| 622 | + ui.KeyEventType type, |
| 623 | + num domTimestamp, |
| 624 | + ) { |
| 625 | + final bool leftPressed = _pressingRecords.containsKey(physicalLeft); |
| 626 | + final bool rightPressed = _pressingRecords.containsKey(physicalRight); |
| 627 | + final bool alreadyPressed = leftPressed || rightPressed; |
| 628 | + final bool synthesizeDown = type == ui.KeyEventType.down && !alreadyPressed; |
| 629 | + final bool synthesizeUp = type == ui.KeyEventType.up && alreadyPressed; |
| 630 | + |
| 631 | + // Synthesize a down event only for the left key if right and left are not pressed |
| 632 | + if (synthesizeDown) { |
| 633 | + _synthesizeKeyDownEvent(domTimestamp, physicalLeft, logicalLeft); |
| 634 | + } |
| 635 | + |
| 636 | + // Synthesize an up event for left key if pressed |
| 637 | + if (synthesizeUp && leftPressed) { |
| 638 | + _synthesizeKeyUpEvent(domTimestamp, physicalLeft, logicalLeft); |
| 639 | + } |
| 640 | + |
| 641 | + // Synthesize an up event for right key if pressed |
| 642 | + if (synthesizeUp && rightPressed) { |
| 643 | + _synthesizeKeyUpEvent(domTimestamp, physicalRight, logicalRight); |
| 644 | + } |
| 645 | + } |
| 646 | + |
| 647 | + void _synthesizeKeyDownEvent(num domTimestamp, int physical, int logical) { |
| 648 | + performDispatchKeyData(ui.KeyData( |
| 649 | + timeStamp: _eventTimeStampToDuration(domTimestamp), |
| 650 | + type: ui.KeyEventType.down, |
| 651 | + physical: physical, |
| 652 | + logical: logical, |
| 653 | + character: null, |
| 654 | + synthesized: true, |
| 655 | + )); |
| 656 | + // Update pressing state |
| 657 | + _pressingRecords[physical] = logical; |
| 658 | + } |
| 659 | + |
| 660 | + void _synthesizeKeyUpEvent(num domTimestamp, int physical, int logical) { |
| 661 | + performDispatchKeyData(ui.KeyData( |
| 662 | + timeStamp: _eventTimeStampToDuration(domTimestamp), |
| 663 | + type: ui.KeyEventType.up, |
| 664 | + physical: physical, |
| 665 | + logical: logical, |
| 666 | + character: null, |
| 667 | + synthesized: true, |
| 668 | + )); |
| 669 | + // Update pressing states |
| 670 | + _pressingRecords.remove(physical); |
| 671 | + } |
| 672 | + |
| 673 | + @visibleForTesting |
| 674 | + bool debugKeyIsPressed(int physical) { |
| 675 | + return _pressingRecords.containsKey(physical); |
| 676 | + } |
670 | 677 | } |
0 commit comments