Skip to content

Handling Events

Emerald edited this page Jun 1, 2024 · 4 revisions

The events state, will allow you to exit your program and handle input.

Handling Input

All you have to do to handle input is create a widget, and take the Events state as input!

fn widget(events: Res<Events>) -> WidgetResult {
   if events.key(KeyCode::Char('q')) {
      ...
   }
}

See! Dead simple.

Exiting a program

In order to exit a program without throwing an error, you have to let the event handler know!

events.register_exit();

However, you have to make sure your program won't hang for an eternity after that, because if it does, the exit will never register!

If you really need an instant exit, your best option would be to throw a super descriptive error!

fn error_widget() -> WidgetResult {
   return Err(anyhow!("Program Exited Because of Super Descriptive Error").into())
}
Clone this wiki locally