KeyMessage class

The assembled information converted from a native key message.

This class is deprecated, and will be removed. There is no direct substitute planned, since this class will no longer be necessary once RawKeyEvent and associated APIs are removed.

Native key messages, produced by physically pressing or releasing keyboard keys, are translated into two different event streams in Flutter:

Either the KeyEvent stream or the RawKeyEvent stream alone provides a complete description of the keyboard messages, but in different event models. Flutter is still transitioning from the legacy model to the new model, therefore it dispatches both streams simultaneously until the transition is completed. KeyMessage is used to bundle the stream segments of both models from a native key message together for the convenience of propagation.

Typically, an application either processes KeyMessage.events or KeyMessage.rawEvent, not both. For example, handling a KeyMessage, means handling each event in KeyMessage.events.

In advanced cases, a widget needs to process both streams at the same time. For example, FocusNode has an onKey that dispatches RawKeyEvents and an onKeyEvent that dispatches KeyEvents. To processes a KeyMessage, it first calls onKeyEvent with each KeyEvent of events, and then onKey with rawEvent. All callbacks are invoked regardless of their KeyEventResult. Their results are combined into the result of the node using combineKeyEventResults.

void handleMessage(FocusNode node, KeyMessage message) {
  final List<KeyEventResult> results = <KeyEventResult>[];
  if (node.onKeyEvent != null) {
    for (final KeyEvent event in message.events) {
      results.add(node.onKeyEvent!(node, event));
    }
  }
  if (node.onKey != null && message.rawEvent != null) {
    results.add(node.onKey!(node, message.rawEvent!));
  }
  final KeyEventResult result = combineKeyEventResults(results);
  // Progress based on `result`...
}
Annotations
  • @Deprecated('No longer supported. Once RawKeyEvent is removed, it will no longer be needed. ' 'This feature was deprecated after v3.18.0-2.0.pre.')
  • @immutable

Constructors

KeyMessage(List<KeyEvent> events, RawKeyEvent? rawEvent)
Create a KeyMessage by providing all information.
const

Properties

events List<KeyEvent>
The list of KeyEvents converted from the native key message.
final
hashCode int
The hash code for this object.
no setterinherited
rawEvent RawKeyEvent?
The native key message in the form of a raw key event.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
override

Operators

operator ==(Object other) bool
The equality operator.
inherited