How to Manage Clipboard Operations Across Platforms with Dear ImGui
Dear ImGui abstracts clipboard handling through the ImGuiPlatformIO structure, exposing two simple helpers—SetClipboardText() and GetClipboardText()—that forward requests to platform-specific function pointers installed by your backend.
Managing clipboard operations across platforms with Dear ImGui requires no OS-specific code in your application logic. The library provides a uniform UTF-8 based API that routes clipboard requests through configurable function pointers stored in the ImGuiPlatformIO structure. This architecture allows standard backends like GLFW, SDL2, and Win32 to inject native clipboard handlers while letting custom platforms provide their own implementations.
The Core Clipboard API in Dear ImGui
Dear ImGui exposes two high-level functions in imgui.h that form the public interface for all clipboard operations:
IMGUI_API void SetClipboardText(const char* text);
IMGUI_API const char* GetClipboardText();
ImGui::SetClipboardText() pushes a null-terminated UTF-8 string to the active clipboard. ImGui::GetClipboardText() retrieves the current clipboard contents as a UTF-8 string, or returns NULL if the operation fails. According to the source code in imgui.h lines 1129-1130, these declarations mark the only two functions most applications need to interact with clipboard data.
Platform Abstraction via ImGuiPlatformIO
Under the hood, the public API delegates to function pointers stored in the ImGuiPlatformIO structure. During ImGui::CreateContext(), Dear ImGui installs default implementations at line 4482 in imgui.cpp:
g.PlatformIO.Platform_SetClipboardTextFn = Platform_SetClipboardTextFn_DefaultImpl;
g.PlatformIO.Platform_GetClipboardTextFn = Platform_GetClipboardTextFn_DefaultImpl;
These defaults provide basic clipboard functionality for rapid prototyping, but real-world applications rely on backends to override them with native OS integration.
Default Implementations by Platform
The default implementations located in imgui.cpp handle common desktop environments:
- Windows: Uses native Win32 clipboard APIs (
OpenClipboard,SetClipboardData,GetClipboardData) - macOS: Calls Application Services frameworks (
PasteboardCreate,PasteboardPutItemFlavor) - Linux/X11: Falls back to an internal "private" clipboard when
IMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCTIONSis defined
Backend-Specific Clipboard Implementations
Production backends replace the default handlers during initialization to ensure zero-copy access and correct ownership semantics. Each backend follows an identical pattern inside its ImGui_ImplXXX_Init() function.
GLFW Backend
In backends/imgui_impl_glfw.cpp lines 695-699, the GLFW backend registers lambda wrappers:
platform_io.Platform_SetClipboardTextFn = [](ImGuiContext*, const char* text) {
glfwSetClipboardString(ImGui_ImplGlfw_GetBackendData()->Window, text);
};
platform_io.Platform_GetClipboardTextFn = [](ImGuiContext*) {
return glfwGetClipboardString(ImGui_ImplGlfw_GetBackendData()->Window);
};
SDL2 Backend
The SDL2 implementation in backends/imgui_impl_sdl2.cpp lines 193-197 handles memory management carefully to prevent leaks:
platform_io.Platform_SetClipboardTextFn = [](ImGuiContext*, const char* text) {
SDL_SetClipboardText(text);
};
platform_io.Platform_GetClipboardTextFn = [](ImGuiContext*) {
char* text = SDL_GetClipboardText();
// ... internal handling to free SDL-allocated memory ...
return text;
};
Win32 Backend
The Win32 backend in imgui_impl_win32.cpp replaces the defaults with direct Win32 clipboard API calls, bypassing the portable defaults entirely to handle Windows-specific edge cases.
How Clipboard Data Flows Through the System
Understanding the execution path helps debug clipboard issues:
- Application calls
ImGui::SetClipboardText("text")orImGui::GetClipboardText() - ImGui core forwards the request to
g.PlatformIO.Platform_SetClipboardTextFnorPlatform_GetClipboardTextFn - Backend function (e.g.,
glfwSetClipboardString) interacts with the OS clipboard API - Return path delivers UTF-8 data back to the application, valid until the next clipboard modification
This architecture ensures that imgui.cpp remains platform-agnostic while ImGuiPlatformIO serves as the bridge to operating system services.
Configuring and Customizing Clipboard Behavior
Beyond standard copy-paste, Dear ImGui provides configuration flags and custom handler support.
Enabling Automatic Window Copy
Set ImGuiIO.ConfigFlags with ConfigWindowsCopyContentsWithCtrlC (defined in imgui.h line 2468) to allow Ctrl+C on focused windows to copy visible text to the clipboard automatically.
Disabling Clipboard Operations
For sandboxed environments or headless applications, clear the platform function pointers:
ImGuiPlatformIO& io = ImGui::GetPlatformIO();
io.Platform_SetClipboardTextFn = nullptr;
io.Platform_GetClipboardTextFn = nullptr;
Once cleared, GetClipboardText() returns NULL and SetClipboardText() becomes a no-op.
Implementing Custom Clipboard Handlers
For custom platforms or embedded systems without standard backends, assign your own functions during initialization:
ImGuiPlatformIO& io = ImGui::GetPlatformIO();
io.Platform_SetClipboardTextFn = [](ImGuiContext*, const char* text) {
// Platform-specific storage mechanism
MyPlatform_SetClipboardUTF8(text);
};
io.Platform_GetClipboardTextFn = [](ImGuiContext*) -> const char* {
static std::string buffer;
buffer = MyPlatform_GetClipboardUTF8();
return buffer.c_str();
};
Thread Safety and Memory Ownership
Memory management semantics vary by backend:
- SDL2: The backend copies
SDL_GetClipboardText()results into ImGui's internal buffer and immediately frees the SDL-allocated memory, preventing leaks (fix added in 2018 perdocs/CHANGELOG.txtline 7747) - GLFW: Returns pointers managed by GLFW that remain valid until the next clipboard change; no additional allocation occurs
- ImGui internal storage: According to
imgui_internal.hline 2557, the context maintains aClipboardHandlerDatabuffer for temporary storage during transfers
Always treat GetClipboardText() return values as transient; copy the data immediately if you need persistent access.
Summary
- Dear ImGui provides portable clipboard operations through
SetClipboardText()andGetClipboardText()inimgui.h - Platform abstraction occurs via
ImGuiPlatformIOfunction pointers set during context creation inimgui.cpp - Standard backends (GLFW, SDL2, Win32) override defaults with native OS integrations to ensure proper UTF-8 handling
- Custom implementations can replace handlers for embedded platforms by assigning lambdas or function pointers to
Platform_SetClipboardTextFnandPlatform_GetClipboardTextFn - Memory ownership varies by backend—SDL2 copies data immediately while GLFW returns OS-managed pointers
Frequently Asked Questions
How do I copy text to the clipboard in Dear ImGui?
Call ImGui::SetClipboardText(const char* text) with a null-terminated UTF-8 string. This immediately forwards the data to the active backend's clipboard handler, which communicates with the operating system.
Why is my custom backend not sharing clipboard data with other applications?
You likely have not set the Platform_SetClipboardTextFn and Platform_GetClipboardTextFn pointers in ImGuiPlatformIO. Without these, Dear ImGui uses an internal "private" clipboard that only exists within your process. Check docs/BACKENDS.md lines 205-206 for the correct registration pattern.
Can I disable clipboard operations entirely in Dear ImGui?
Yes. Set both Platform_SetClipboardTextFn and Platform_GetClipboardTextFn in ImGuiPlatformIO to nullptr after initializing ImGui. This prevents any clipboard access, which is useful for sandboxed environments or when running automated tests.
Does Dear ImGui handle Unicode and UTF-8 correctly on all platforms?
Yes. The clipboard API exclusively uses UTF-8 encoding. Backend implementations handle the conversion to platform-specific formats (such as UTF-16 on Windows) internally, so your application code always sends and receives standard UTF-8 C-strings.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →