Implementation
KeyEventResult onKey(RawKeyEvent e) {
if (e is! RawKeyDownEvent) {
if (e is RawKeyUpEvent && e.physicalKey == _physicalKeyThatIsDown) {
releasePressedButton();
_physicalKeyThatIsDown = null;
}
return KeyEventResult.ignored;
}
final now = DateTime.now();
if (e.physicalKey == _physicalKeyThatIsDown &&
now.difference(lastKeyDown).inMilliseconds < 2000) {
// Effectively, disable autorepeat.
lastKeyDown = now;
return KeyEventResult.handled;
}
final Characters ch;
if (e.physicalKey == PhysicalKeyboardKey.enter ||
e.physicalKey == PhysicalKeyboardKey.numpadEnter) {
// Bizarrely, on the web we get 'E' as the character in this case!
// https://github.com/flutter/flutter/issues/82065
ch = Characters('\n');
} else if (e.physicalKey == PhysicalKeyboardKey.delete ||
e.physicalKey == PhysicalKeyboardKey.backspace) {
ch = Characters('\u0008');
} else if (_ignored.contains(e.physicalKey)) {
// Other keys give weird results on the web, alas. Like, control
// gives "C". I didn't file a bug, but I may post a flaming screed
// to alt.javascript.die.die.die
return KeyEventResult.ignored;
} else {
String? sch = e.character;
if (sch == null) {
return KeyEventResult.ignored;
}
if (e.isControlPressed && (sch == 'f' || sch == 'F')) {
// Yes, JavaScript really does suck. In case you were wondering.
ch = Characters('\u0006');
} else if (e.isControlPressed && (sch == 'g' || sch == 'G')) {
ch = Characters('\u0007');
} else {
ch = Characters(sch).toUpperCase();
}
if (ch.isEmpty) {
return KeyEventResult.ignored;
}
}
final CalculatorButtonState? b = button[ch.first];
if (b != null) {
releasePressedButton(); // Just in case, probably does nothing
if (e.character == '<' || e.character == '>') {
final gShift = button['G'];
assert(gShift != null);
if (gShift != null) {
// Shut up analyzer
gShift.keyPressed();
_extraShiftThatIsDown = gShift;
}
}
b.keyPressed();
_buttonThatIsDown = b;
_physicalKeyThatIsDown = e.physicalKey;
lastKeyDown = now;
return KeyEventResult.handled;
} else if (e.character == '?') {
controller.model.settings.showAccelerators.value =
!controller.model.settings.showAccelerators.value;
_physicalKeyThatIsDown = e.physicalKey;
lastKeyDown = now;
return KeyEventResult.handled;
}
return KeyEventResult.ignored;
}