handler.rs 762 B

1234567891011121314151617181920212223242526
  1. use crate::app::{App, AppResult};
  2. use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
  3. /// Handles the key events and updates the state of [`App`].
  4. pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
  5. match key_event.code {
  6. // Exit application on `ESC` or `q`
  7. KeyCode::Esc | KeyCode::Char('q') => {
  8. app.quit();
  9. }
  10. // Exit application on `Ctrl-C`
  11. KeyCode::Char('c') | KeyCode::Char('C') => {
  12. if key_event.modifiers == KeyModifiers::CONTROL {
  13. app.quit();
  14. }
  15. }
  16. KeyCode::Down => {
  17. app.lumps.next();
  18. }
  19. KeyCode::Up => {
  20. app.lumps.previous();
  21. }
  22. _ => {}
  23. }
  24. Ok(())
  25. }