logicalKey property

  1. @override
LogicalKeyboardKey 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 {
  // 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 = kIosNumPadMap[keyCode];
  if (numPadKey != null) {
    return numPadKey;
  }

  // Look to see if the [keyLabel] is one we know about and have a mapping for.
  final LogicalKeyboardKey? specialKey = kIosSpecialLogicalMap[keyLabel];
  if (specialKey != null) {
    return specialKey;
  }

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

  // If this key is printable, 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.
  if (keyLabel.isNotEmpty &&
      !LogicalKeyboardKey.isControlCharacter(keyLabel) &&
      !_isUnprintableKey(keyLabel)) {
    // Given that charactersIgnoringModifiers can contain a String of
    // arbitrary length, limit to a maximum of two Unicode scalar values. It
    // is unlikely that a keyboard would produce a code point bigger than 32
    // bits, but it is still worth defending against this case.
    assert(charactersIgnoringModifiers.length <= 2);
    int codeUnit = charactersIgnoringModifiers.codeUnitAt(0);
    if (charactersIgnoringModifiers.length == 2) {
      final int secondCode = charactersIgnoringModifiers.codeUnitAt(1);
      codeUnit = (codeUnit << 16) | secondCode;
    }

    final int keyId = LogicalKeyboardKey.unicodePlane | (codeUnit & 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.iosPlane);
}