FAQ - Help and questions

How do I copy and paste in an egui app?

#egui#clipboard#eframepublished 0 replies0 views
ReplyWatch
Share
Hacker NewsRedditLinkedInE-mail

Short answer

Text boxes in egui already support copy, cut and paste. For your own button, call ctx.copy_text with the string. To react to a paste anywhere in the window, read the egui Event Paste values from the input each frame. The code passed cargo check with eframe 0.35.0.

Checked with rustc 1.93.0, Windows 11, eframe 0.35.0; `cargo check` only, the window was not opened and the clipboard was not touched.

Frequently asked question, written and answered by the Ruststudio team. Replies are open: corrections are welcome.

#1

Answer#

Inside an egui window you do not need a separate clipboard crate:

  • Copy from your own button: ui.ctx().copy_text(string) asks egui to put the text on the system clipboard.
  • Paste anywhere: when the user presses Ctrl+V (Cmd+V on macOS), egui delivers an egui::Event::Paste(String) in the frame's input events.
  • Text boxes: TextEdit handles select, copy, cut and paste for itself.
src/main.rsrust
use eframe::egui;

#[derive(Default)]
struct App {
    text: String,
    pasted: String,
}

impl eframe::App for App {
    fn ui(&mut self, ui: &mut egui::Ui, _frame: &mut eframe::Frame) {
        // Text arrives as an event when the user presses Ctrl+V (Cmd+V on macOS) in the window.
        ui.ctx().input(|i| {
            for event in &i.events {
                if let egui::Event::Paste(s) = event {
                    self.pasted = s.clone();
                }
            }
        });
        egui::CentralPanel::default().show(ui, |ui| {
            ui.text_edit_singleline(&mut self.text); // copy, cut and paste already work here
            if ui.button("Copy the text above").clicked() {
                ui.ctx().copy_text(self.text.clone());
            }
            ui.label(format!("Last paste event: {}", self.pasted));
        });
    }
}

fn main() -> eframe::Result {
    eframe::run_native("Clipboard", eframe::NativeOptions::default(), Box::new(|_cc| Ok(Box::new(App::default()))))
}

This compiled with cargo check on Windows 11 using eframe 0.35.0. It was not run.

When the focus is in a text box, that box consumes the paste for itself; the app-level Event::Paste is for shortcuts such as "paste a file path anywhere to open it".

Common mistakes#

  • Using a clipboard crate and egui's copy_text together without a reason. Pick one path so the behaviour is predictable.
  • Expecting copy_text to return an error. It does not; if you must know that the copy worked, use a crate that returns a Result.
  • Pasting large text into a String shown by Label each frame. Cut long text to what fits.

How do I read the clipboard without a key press?#

egui's event model gives you pastes the user asks for. Reading on demand needs a crate such as arboard; see its thread.

Sources#

    QuoteReply
    Share
    Hacker NewsRedditLinkedInE-mail

    No replies yet