KeyMap class

A collection of key bindings forming a key map.

Use this to group related bindings for help views and dispatch. Handlers are placed directly on each KeyBinding via the handler parameter.

Basic usage (subclass)

class MyKeyMap extends KeyMap {
  MyKeyMap() {
    shortHelp = [up, down, quit];
    fullHelp = [
      [up, down],
      [quit],
    ];
  }

  final up = KeyBinding.withHelp(['up', 'k'], '↑/k', 'move up');
  final down = KeyBinding.withHelp(['down', 'j'], '↓/j', 'move down');
  final quit = KeyBinding.withHelp(['q', 'ctrl+c'], 'q', 'quit');
}

With handlers

class AppKeyMap extends KeyMap {
  AppKeyMap() {
    shortHelp = [quit, help];
    fullHelp = [[quit], [help]];
  }

  final quit = KeyBinding(
    keys: ['q'],
    help: Help(key: 'q', desc: 'quit'),
    handler: () => Cmd.quit(),
  );
  final help = KeyBinding(
    keys: ['?'],
    help: Help(key: '?', desc: 'toggle help'),
    handler: () => Cmd.message(ToggleHelpMsg()),
  );
}

In your model:

(Model, Cmd?) update(Msg msg) {
  if (msg case KeyMsg()) {
    final cmd = keyMap.handle(msg);
    if (cmd != null) return (this, cmd);
  }
  return (this, null);
}
Implementers

Constructors

KeyMap({List<KeyBinding>? shortHelp, List<List<KeyBinding>>? fullHelp, List<({String id, KeyBinding key, KeyBinding prefix})>? chords})
Creates a new key map.
KeyMap.simple(List<KeyBinding> bindings)
Creates a key map with bindings set as both short and full help.
factory

Properties

chords List<({String id, KeyBinding key, KeyBinding prefix})>?
Optional multi-key chord bindings.
getter/setter pair
fullHelp List<List<KeyBinding>>
Bindings for the full help view, grouped by columns.
getter/setter pair
hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
shortHelp List<KeyBinding>
Bindings for the short help view.
getter/setter pair

Methods

firstMatch(KeyMsg msg) KeyBinding?
Returns the first enabled KeyBinding that matches msg, or null.
handle(KeyMsg msg) Cmd?
Returns the command from the first matching handler, or null.
intercept(KeyMsg msg) bool
Returns true if a matching binding's KeyBinding.action was called.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

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