isModifierPressed method Null safety

  1. @override
bool isModifierPressed(
  1. ModifierKey key,
  2. {KeyboardSide side = KeyboardSide.any}
)
override

Returns true if the given ModifierKey was pressed at the time of this event.

If side is specified, then this restricts its check to the specified side of the keyboard. Defaults to checking for the key being down on either side of the keyboard. If there is only one instance of the key on the keyboard, then side is ignored.

Implementation

@override
bool isModifierPressed(ModifierKey key, {KeyboardSide side = KeyboardSide.any}) {
  final int independentModifier = modifiers & deviceIndependentMask;
  final bool result;
  switch (key) {
    case ModifierKey.controlModifier:
      result = _isLeftRightModifierPressed(side, independentModifier & modifierControl, modifierLeftControl, modifierRightControl);
      break;
    case ModifierKey.shiftModifier:
      result = _isLeftRightModifierPressed(side, independentModifier & modifierShift, modifierLeftShift, modifierRightShift);
      break;
    case ModifierKey.altModifier:
      result = _isLeftRightModifierPressed(side, independentModifier & modifierOption, modifierLeftOption, modifierRightOption);
      break;
    case ModifierKey.metaModifier:
      result = _isLeftRightModifierPressed(side, independentModifier & modifierCommand, modifierLeftCommand, modifierRightCommand);
      break;
    case ModifierKey.capsLockModifier:
      result = independentModifier & modifierCapsLock != 0;
      break;
  // On macOS, the function modifier bit is set for any function key, like F1,
  // F2, etc., but the meaning of ModifierKey.modifierFunction in Flutter is
  // that of the Fn modifier key, so there's no good way to emulate that on
  // macOS.
    case ModifierKey.functionModifier:
    case ModifierKey.numLockModifier:
    case ModifierKey.symbolModifier:
    case ModifierKey.scrollLockModifier:
      // These modifier masks are not used in macOS keyboards.
      result = false;
      break;
  }
  assert(!result || getModifierSide(key) != null, "$runtimeType thinks that a modifier is pressed, but can't figure out what side it's on.");
  return result;
}