Skip to content

How do I "handle" an event in a control? #1124

Answered by emilk
bellwether-softworks asked this question in Q&A
Discussion options

You must be logged in to vote

Good question! I haven't thought through this use case much yet. I think the only solution is to catch the events before running the TextEdit code. Perhaps something like this:

let text_edit_id = Id::new("my_code_editor");
let editor_has_focus = ui.ctx().memory().has_focus(self.id);
if editor_has_focus && events.iter().any(|evt|
    matches!(evt,
        Event::Key { key, modifiers, pressed }
        if key == &Key::Enter && *pressed && modifiers.ctrl
    )) {
    ui.ctx().memory().surrender_focus(self.id);
    perform_behavior();
}

let editor = ui.add(
    egui::TextEdit::multiline(&mut editor_text)
        .id(text_edit_id)
        .code_editor()
        .desired_width(f32::INFINITY)
);

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by bellwether-softworks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants