getModifierSide method Null safety

  1. @override
KeyboardSide? getModifierSide(
  1. ModifierKey key

Returns a KeyboardSide enum value that describes which side or sides of the given keyboard modifier key were pressed at the time of this event.

If the modifier key wasn't pressed at the time of this event, returns null. If the given key only appears in one place on the keyboard, returns KeyboardSide.all if pressed. Never returns KeyboardSide.any, because that doesn't make sense in this context.


KeyboardSide? getModifierSide(ModifierKey key) {
  KeyboardSide? findSide(int anyMask, int leftMask, int rightMask) {
    final int combinedMask = leftMask | rightMask;
    final int combined = modifiers & combinedMask;
    if (combined == leftMask) {
      return KeyboardSide.left;
    } else if (combined == rightMask) {
      return KeyboardSide.right;
    } else if (combined == combinedMask || modifiers & (combinedMask | anyMask) == anyMask) {
      // Handles the case where macOS supplies just the "either" modifier
      // flag, but not the left/right flag. (e.g. modifierShift but not
      // modifierLeftShift), or if left and right flags are provided, but not
      // the "either" modifier flag.
      return KeyboardSide.all;
    return null;

  switch (key) {
    case ModifierKey.controlModifier:
      return findSide(modifierControl, modifierLeftControl, modifierRightControl);
    case ModifierKey.shiftModifier:
      return findSide(modifierShift, modifierLeftShift, modifierRightShift);
    case ModifierKey.altModifier:
      return findSide(modifierOption, modifierLeftOption, modifierRightOption);
    case ModifierKey.metaModifier:
      return findSide(modifierCommand, modifierLeftCommand, modifierRightCommand);
    case ModifierKey.capsLockModifier:
    case ModifierKey.numLockModifier:
    case ModifierKey.scrollLockModifier:
    case ModifierKey.functionModifier:
    case ModifierKey.symbolModifier:
      return KeyboardSide.all;