Handle keys and move focus
Recipe for handle_key, consuming events, and Tab focus.
Handle keys and move focus
Problem: route keystrokes to the right widget and stop them propagating.
Steps:
- Match on
key.codeinhandle_keyand returntruefor every key you consume. Unconsumed keys fall through to global routing:
fn handle_key(&mut self, key: KeyEvent, cx: &mut Context<Action>) -> bool {
match key.code {
KeyCode::Char('d') => { cx.dispatch(Action::DeployRequested); true }
_ => false,
}
}
- For text fields, own a
TextInputin view state and forward the key:
match key.code {
KeyCode::Tab => { self.focus = (self.focus + 1) % 2; true }
_ => self.focused_input_mut().handle_key(&key),
}
TextInput::handle_keycoversCharinsert,Backspace, arrows,Home, andEnd. The cursor is char-indexed, so multi-byte input edits correctly.
Notes: q is reserved as the global quit key by VeloceApp::run — don’t
bind application behavior to it.