keyEventHandler property Null safety

RawKeyEventHandler? keyEventHandler

A handler for raw hardware keyboard events that will stop propagation if the handler returns true.

This property is only a wrapper over KeyEventManager.keyMessageHandler, and is kept only for backward compatibility. New code should use KeyEventManager.keyMessageHandler to set custom global key event handler. Setting keyEventHandler will cause KeyEventManager.keyMessageHandler to be set with a converted handler. If KeyEventManager.keyMessageHandler is set by FocusManager (the most common situation), then the exact value of keyEventHandler is a dummy callback and must not be invoked.

Implementation

RawKeyEventHandler? get keyEventHandler {
  if (ServicesBinding.instance.keyEventManager.keyMessageHandler != _cachedKeyMessageHandler) {
    _cachedKeyMessageHandler = ServicesBinding.instance.keyEventManager.keyMessageHandler;
    _cachedKeyEventHandler = _cachedKeyMessageHandler == null ?
      null :
      (RawKeyEvent event) {
        assert(false,
            'The RawKeyboard.instance.keyEventHandler assigned by Flutter is a dummy '
            'callback kept for compatibility and should not be directly called. Use '
            'ServicesBinding.instance!.keyMessageHandler instead.');
        return true;
      };
  }
  return _cachedKeyEventHandler;
}
void keyEventHandler=(RawKeyEventHandler? handler)

Implementation

set keyEventHandler(RawKeyEventHandler? handler) {
  _cachedKeyEventHandler = handler;
  _cachedKeyMessageHandler = handler == null ?
    null :
    (KeyMessage message) {
      if (message.rawEvent != null) {
        return handler(message.rawEvent!);
      }
      return false;
    };
  ServicesBinding.instance.keyEventManager.keyMessageHandler = _cachedKeyMessageHandler;
}