Add option to turn off the default clipboard keyboard shortcuts in egui-winit and eframe (#5615)

* Closes <https://github.com/emilk/egui/issues/4065>
* Closes <https://github.com/emilk/egui/issues/5536>
* [x] I have followed the instructions in the PR template

---------

Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
This commit is contained in:
YgorSouzaandEmil Ernerfeldt authored and GitHub committed 2026-10-06 08:42:22 +01:00
1 parent 9ebef5d10e
commit 96ae24bb90
4 files changed
+90 -8

No files matched your search

+21
View File
@@ -403,6 +403,25 @@ pub struct NativeOptions {
/// Defaults to true.
pub dithering: bool,
/// Should the platform clipboard keyboard shortcuts
/// (e.g. <kbd>Cmd/Ctrl</kbd>+<kbd>X</kbd>/<kbd>C</kbd>/<kbd>V</kbd>)
/// be translated into [`egui::Event::Cut`], [`egui::Event::Copy`] and [`egui::Event::Paste`]?
///
/// Set this to `false` if you want to handle these key combinations yourself.
/// They will then arrive as ordinary [`egui::Event::Key`] events instead.
/// Note that built-in widgets such as [`egui::TextEdit`] will then no longer
/// respond to these shortcuts.
///
/// This applies to all viewports.
/// See also `egui_winit::State::set_clipboard_shortcuts`.
///
/// There is no web equivalent: on the web, Cut/Copy/Paste come from the browser's
/// clipboard events (which can also be triggered from e.g. the browser menu),
/// and the key presses are always also delivered as [`egui::Event::Key`].
///
/// Defaults to true.
pub clipboard_shortcuts: bool,
/// Android application for `winit`'s event loop.
///
/// This value is required on Android to correctly create the event loop. See
@@ -480,6 +499,8 @@ impl Default for NativeOptions {
dithering: true,
clipboard_shortcuts: true,
#[cfg(target_os = "android")]
android_app: None,
}
+8 -2
View File
@@ -108,6 +108,9 @@ struct GlutinWindowContext {
max_texture_side: Option<usize>,
/// Passed on to each [`egui_winit::State`]. See [`NativeOptions::clipboard_shortcuts`].
clipboard_shortcuts: bool,
current_gl_context: Option<glutin::context::PossiblyCurrentContext>,
not_current_gl_context: Option<glutin::context::NotCurrentContext>,
@@ -1300,6 +1303,7 @@ impl GlutinWindowContext {
viewports,
viewport_from_window,
max_texture_side: None,
clipboard_shortcuts: native_options.clipboard_shortcuts,
window_from_viewport,
focused_viewport: Some(ViewportId::ROOT),
};
@@ -1354,14 +1358,16 @@ impl GlutinWindowContext {
viewport.egui_winit.get_or_insert_with(|| {
log::debug!("Initializing egui_winit for viewport {viewport_id:?}");
egui_winit::State::new(
let mut egui_winit = egui_winit::State::new(
self.egui_ctx.clone(),
viewport_id,
event_loop,
Some(window.scale_factor() as f32),
event_loop.system_theme(),
self.max_texture_side,
)
);
egui_winit.set_clipboard_shortcuts(self.clipboard_shortcuts);
egui_winit
});
if viewport.gl_surface.is_none() {
+28 -5
View File
@@ -90,6 +90,9 @@ pub struct SharedState {
viewport_from_window: HashMap<WindowId, ViewportId>,
focused_viewport: Option<ViewportId>,
resized_viewport: Option<ViewportId>,
/// Passed on to each [`egui_winit::State`]. See [`NativeOptions::clipboard_shortcuts`].
clipboard_shortcuts: bool,
}
pub type Viewports = egui::OrderedViewportIdMap<Viewport>;
@@ -161,6 +164,7 @@ impl<'app> WgpuWinitApp<'app> {
viewports,
painter,
viewport_from_window,
clipboard_shortcuts,
..
} = &mut *shared;
@@ -170,6 +174,7 @@ impl<'app> WgpuWinitApp<'app> {
&running.integration.egui_ctx,
viewport_from_window,
painter,
*clipboard_shortcuts,
);
}
}
@@ -181,6 +186,7 @@ impl<'app> WgpuWinitApp<'app> {
viewports,
viewport_from_window,
painter,
clipboard_shortcuts,
..
} = &mut *running.shared.borrow_mut();
@@ -192,7 +198,13 @@ impl<'app> WgpuWinitApp<'app> {
None,
painter,
)
.initialize_window(event_loop, egui_ctx, viewport_from_window, painter);
.initialize_window(
event_loop,
egui_ctx,
viewport_from_window,
painter,
*clipboard_shortcuts,
);
}
#[cfg(target_os = "android")]
@@ -294,7 +306,6 @@ impl<'app> WgpuWinitApp<'app> {
});
}
#[allow(clippy::allow_attributes, unused_mut)] // used for accesskit
let mut egui_winit = egui_winit::State::new(
egui_ctx.clone(),
ViewportId::ROOT,
@@ -303,6 +314,7 @@ impl<'app> WgpuWinitApp<'app> {
event_loop.system_theme(),
painter.max_texture_side(),
);
egui_winit.set_clipboard_shortcuts(self.native_options.clipboard_shortcuts);
#[cfg(feature = "accesskit")]
{
@@ -360,6 +372,7 @@ impl<'app> WgpuWinitApp<'app> {
painter,
focused_viewport: Some(ViewportId::ROOT),
resized_viewport: None,
clipboard_shortcuts: self.native_options.clipboard_shortcuts,
}));
{
@@ -1076,6 +1089,7 @@ impl Viewport {
egui_ctx: &egui::Context,
windows_id: &mut HashMap<WindowId, ViewportId>,
painter: &mut egui_wgpu::winit::Painter,
clipboard_shortcuts: bool,
) {
if self.window.is_some() {
return; // we already have one
@@ -1097,14 +1111,16 @@ impl Viewport {
log::error!("on set_window: viewport_id {viewport_id:?} {err}");
}
self.egui_winit = Some(egui_winit::State::new(
let mut egui_winit = egui_winit::State::new(
egui_ctx.clone(),
viewport_id,
event_loop,
Some(window.scale_factor() as f32),
event_loop.system_theme(),
painter.max_texture_side(),
));
);
egui_winit.set_clipboard_shortcuts(clipboard_shortcuts);
self.egui_winit = Some(egui_winit);
egui_winit::update_viewport_info(&mut self.info, egui_ctx, &window, true);
self.window = Some(window);
@@ -1176,6 +1192,7 @@ fn render_immediate_viewport(
viewports,
painter,
viewport_from_window,
clipboard_shortcuts,
..
} = &mut *shared.borrow_mut();
@@ -1189,7 +1206,13 @@ fn render_immediate_viewport(
);
if viewport.window.is_none() {
event_loop_context::with_current_event_loop(|event_loop| {
viewport.initialize_window(event_loop, egui_ctx, viewport_from_window, painter);
viewport.initialize_window(
event_loop,
egui_ctx,
viewport_from_window,
painter,
*clipboard_shortcuts,
);
});
}
+33 -1
View File
@@ -129,6 +129,10 @@ pub struct State {
pub accesskit: Option<accesskit_winit::Adapter>,
allow_ime: bool,
/// See [`Self::set_clipboard_shortcuts`].
clipboard_shortcuts: bool,
ime_rect_px: Option<egui::Rect>,
old_ime_purpose: egui::IMEPurpose,
@@ -190,6 +194,7 @@ impl State {
accesskit: None,
allow_ime: false,
clipboard_shortcuts: true,
ime_rect_px: None,
old_ime_purpose: egui::IMEPurpose::Normal,
#[cfg(target_os = "macos")]
@@ -259,6 +264,33 @@ impl State {
self.allow_ime = allow;
}
/// Are clipboard keyboard shortcuts translated into clipboard events?
///
/// See [`Self::set_clipboard_shortcuts`].
#[inline]
pub fn clipboard_shortcuts(&self) -> bool {
self.clipboard_shortcuts
}
/// Should the platform clipboard keyboard shortcuts be translated into
/// [`egui::Event::Cut`], [`egui::Event::Copy`] and [`egui::Event::Paste`]?
///
/// The shortcuts are <kbd>Cmd/Ctrl</kbd>+<kbd>X</kbd>/<kbd>C</kbd>/<kbd>V</kbd>,
/// the dedicated Cut/Copy/Paste keys, and on Windows also
/// <kbd>Shift</kbd>+<kbd>Delete</kbd>, <kbd>Ctrl</kbd>+<kbd>Insert</kbd> and <kbd>Shift</kbd>+<kbd>Insert</kbd>.
///
/// If `false`, these key presses are instead passed on as ordinary [`egui::Event::Key`] events,
/// so your app can bind them to whatever it wants.
/// Note that this means built-in widgets such as [`egui::TextEdit`] will no longer
/// respond to these shortcuts, since they listen for the clipboard events.
/// You can still push [`egui::Event::Cut`] etc. yourself.
///
/// Default: `true`.
#[inline]
pub fn set_clipboard_shortcuts(&mut self, enabled: bool) {
self.clipboard_shortcuts = enabled;
}
#[inline]
pub fn egui_ctx(&self) -> &egui::Context {
&self.egui_ctx
@@ -1083,7 +1115,7 @@ impl State {
// are mapped to the physical keys that normally contain C, X, V, etc.
// See also: https://github.com/emilk/egui/issues/3653
if let Some(active_key) = logical_key.or(physical_key) {
if pressed {
if pressed && self.clipboard_shortcuts {
if is_cut_command(self.modifiers, active_key) {
self.egui_input.events.push(egui::Event::Cut);
return;