import 'package:flutter/services.dart'; import 'package:flutter/widgets.dart'; /// The chat quick switcher's keys, read while its field has the keyboard: /// Ctrl+N and Down step the selection forward ([onStep] with 1), Ctrl+P and /// Up back (-1), and Enter chooses it ([onChoose]). Control rather than Cmd /// on Apple platforms too, as in Search and Emacs-style lists. /// /// The chords are reported handled, also with nothing to select, so a /// browser does not run its own default (Ctrl+P prints). A browser tab may /// still keep Ctrl+N for itself (a new window) before the page sees it; Up /// and Down always work, and an installed app window or the terminal's /// "Capture browser keys" full screen passes Ctrl+N through. KeyEventResult chatQuickSwitchKey( KeyEvent event, HardwareKeyboard keyboard, { required ValueChanged onStep, required VoidCallback onChoose, }) { if (event is! KeyDownEvent && event is! KeyRepeatEvent) { return KeyEventResult.ignored; } if (keyboard.isAltPressed || keyboard.isMetaPressed) { return KeyEventResult.ignored; } final key = event.logicalKey; final control = keyboard.isControlPressed; final shift = keyboard.isShiftPressed; final step = switch (key) { LogicalKeyboardKey.keyN when control && !shift => 1, LogicalKeyboardKey.keyP when control && !shift => -1, LogicalKeyboardKey.arrowDown when !control && !shift => 1, LogicalKeyboardKey.arrowUp when !control && !shift => -1, _ => 0, }; if (step != 0) { onStep(step); return KeyEventResult.handled; } if (event is KeyDownEvent && !control && !shift && (key == LogicalKeyboardKey.enter || key == LogicalKeyboardKey.numpadEnter)) { onChoose(); return KeyEventResult.handled; } return KeyEventResult.ignored; }