logicalKey property

  1. @override
LogicalKeyboardKey get logicalKey
override

Returns an object representing the logical key that was pressed.

This method takes into account the key map and modifier keys (like SHIFT) to determine which logical key to return.

If you are looking for the character produced by a key event, use RawKeyEvent.character instead.

If you are collecting text strings, use the TextField or CupertinoTextField widgets, since those automatically handle many of the complexities of managing keyboard input, like showing a soft keyboard or interacting with an input method editor (IME).

See also:

Implementation

@override
LogicalKeyboardKey get logicalKey {
  if (specifiedLogicalKey != null) {
    final int key = specifiedLogicalKey!;
    return LogicalKeyboardKey.findKeyByKeyId(key) ?? LogicalKeyboardKey(key);
  }
  // Look to see if the keyCode is a printable number pad key, so that a
  // difference between regular keys (e.g. "=") and the number pad version
  // (e.g. the "=" on the number pad) can be determined.
  final LogicalKeyboardKey? numPadKey = kMacOsNumPadMap[keyCode];
  if (numPadKey != null) {
    return numPadKey;
  }

  // Keys that can't be derived with characterIgnoringModifiers will be
  // derived from their key codes using this map.
  final LogicalKeyboardKey? knownKey = kMacOsToLogicalKey[keyCode];
  if (knownKey != null) {
    return knownKey;
  }

  // If this key is a single printable character, generate the
  // LogicalKeyboardKey from its Unicode value. Control keys such as ESC,
  // CTRL, and SHIFT are not printable. HOME, DEL, arrow keys, and function
  // keys are considered modifier function keys, which generate invalid
  // Unicode scalar values. Multi-char characters are also discarded.
  int? character;
  if (keyLabel.isNotEmpty) {
    final List<int> codePoints = keyLabel.runes.toList();
    if (codePoints.length == 1 &&
        // Ideally we should test whether `codePoints[0]` is in the range.
        // Since LogicalKeyboardKey.isControlCharacter and _isUnprintableKey
        // only tests BMP, it is fine to test keyLabel instead.
        !LogicalKeyboardKey.isControlCharacter(keyLabel) &&
        !_isUnprintableKey(keyLabel)) {
      character = runeToLowerCase(codePoints[0]);
    }
  }
  if (character != null) {
    final int keyId = LogicalKeyboardKey.unicodePlane | (character & LogicalKeyboardKey.valueMask);
    return LogicalKeyboardKey.findKeyByKeyId(keyId) ?? LogicalKeyboardKey(keyId);
  }

  // This is a non-printable key that we don't know about, so we mint a new
  // code.
  return LogicalKeyboardKey(keyCode | LogicalKeyboardKey.macosPlane);
}