Listen to events

Often you want to do something when the user presses some key, if a file is dropped on your window or general mouse events. For that, you can use the iced::event::listen subscription. It runs in the background and emits a message with/on every Event.

Note: If you just want to get mouse events in a specific widget area you should use the MouseArea widget.

Here is a practical example how to listen to an arbitrary event in form of a keyboard event.

In the example, the subscription always runs as defined in the .subscription method of application here:

use iced::widget;

#[derive(Debug, Clone)]
enum Message {
    Event(iced::event::Event),
}

fn update(_state: &mut u8, message: Message) -> iced::Task<Message> {
    // handle emitted messages
    match message {
        Message::Event(event) => match event {
            iced::event::Event::Keyboard(keyboard_event) => match keyboard_event {
                iced::keyboard::Event::KeyReleased { key, .. } => {
                    println!("Key {:?} was pressed", key);
                }
                _ => {}
            },
            _ => {}
        },
    }
    iced::Task::none()
}

fn view(_state: &u8) -> iced::Element<'_, Message> {
    widget::text("Event Example").into()
}

fn main() -> Result<(), iced::Error> {
    // run the app from main function
    iced::application("Event example", update, view)
        .subscription(|_state| iced::event::listen().map(Message::Event))
        .run()
}

It emits a message containing the event:

use iced::widget;

#[derive(Debug, Clone)]
enum Message {
    Event(iced::event::Event),
}

fn update(_state: &mut u8, message: Message) -> iced::Task<Message> {
    // handle emitted messages
    match message {
        Message::Event(event) => match event {
            iced::event::Event::Keyboard(keyboard_event) => match keyboard_event {
                iced::keyboard::Event::KeyReleased { key, .. } => {
                    println!("Key {:?} was pressed", key);
                }
                _ => {}
            },
            _ => {}
        },
    }
    iced::Task::none()
}

fn view(_state: &u8) -> iced::Element<'_, Message> {
    widget::text("Event Example").into()
}

fn main() -> Result<(), iced::Error> {
    // run the app from main function
    iced::application("Event example", update, view)
        .subscription(|_state| iced::event::listen().map(Message::Event))
        .run()
}

In the update method we can use that event and react to it:

use iced::widget;

#[derive(Debug, Clone)]
enum Message {
    Event(iced::event::Event),
}

fn update(_state: &mut u8, message: Message) -> iced::Task<Message> {
    // handle emitted messages
    match message {
        Message::Event(event) => match event {
            iced::event::Event::Keyboard(keyboard_event) => match keyboard_event {
                iced::keyboard::Event::KeyReleased { key, .. } => {
                    println!("Key {:?} was pressed", key);
                }
                _ => {}
            },
            _ => {}
        },
    }
    iced::Task::none()
}

fn view(_state: &u8) -> iced::Element<'_, Message> {
    widget::text("Event Example").into()
}

fn main() -> Result<(), iced::Error> {
    // run the app from main function
    iced::application("Event example", update, view)
        .subscription(|_state| iced::event::listen().map(Message::Event))
        .run()
}

Important Note: The example uses iced::event::listen that reacts to all kind of events. There are specific subscriptions for special event kinds, such as window, key_press and key_release, as well.

Full Code

use iced::widget;

#[derive(Debug, Clone)]
enum Message {
    Event(iced::event::Event),
}

fn update(_state: &mut u8, message: Message) -> iced::Task<Message> {
    // handle emitted messages
    match message {
        Message::Event(event) => match event {
            iced::event::Event::Keyboard(keyboard_event) => match keyboard_event {
                iced::keyboard::Event::KeyReleased { key, .. } => {
                    println!("Key {:?} was pressed", key);
                }
                _ => {}
            },
            _ => {}
        },
    }
    iced::Task::none()
}

fn view(_state: &u8) -> iced::Element<'_, Message> {
    widget::text("Event Example").into()
}

fn main() -> Result<(), iced::Error> {
    // run the app from main function
    iced::application("Event example", update, view)
        .subscription(|_state| iced::event::listen().map(Message::Event))
        .run()
}