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:
TextEdithandles select, copy, cut and paste for itself.
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_texttogether without a reason. Pick one path so the behaviour is predictable. - Expecting
copy_textto return an error. It does not; if you must know that the copy worked, use a crate that returns aResult. - Pasting large text into a
Stringshown byLabeleach frame. Cut long text to what fits.
Related questions#
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#
- egui Context::copy_text: https://docs.rs/egui/0.35.0/egui/struct.Context.html#method.copy_text (read 2026-12-05)
- egui Event: https://docs.rs/egui/0.35.0/egui/enum.Event.html (read 2026-12-05)