1+ /*******************************************************************************************
2+ *
3+ * raylib [core] example - clipboard text
4+ *
5+ * Example complexity rating: [★☆☆☆] 1/4
6+ *
7+ * Example originally created with raylib 5.6-dev
8+ *
9+ * Example contributed by Robin (@RobinsAviary) and reviewed by Ramon Santamaria (@raysan5)
10+ *
11+ * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
12+ * BSD-like license that allows static linking with closed source software
13+ *
14+ * Copyright (c) 2025-2025 Robin (@RobinsAviary)
15+ *
16+ ********************************************************************************************/
17+
18+ #include "raylib.h"
19+
20+ #include <stdio.h>
21+
22+ //------------------------------------------------------------------------------------
23+ // Program main entry point
24+ //------------------------------------------------------------------------------------
25+ int main (void )
26+ {
27+ // Initialization
28+ //--------------------------------------------------------------------------------------
29+ const int screenWidth = 800 ;
30+ const int screenHeight = 450 ;
31+
32+ InitWindow (screenWidth , screenHeight , "raylib [core] example - clipboard text" );
33+
34+ const char * clipboardText = NULL ;
35+
36+ // List of text the user can switch through and copy
37+ const char * copyableText [] = {"raylib is fun" , "hello, clipboard!" , "potato chips" };
38+
39+ unsigned int textIndex = 0 ;
40+
41+ const char * popupText = NULL ;
42+
43+ // Initialize timers
44+ // The amount of time the pop-up text is on screen, before fading
45+ const float maxTime = 3.0f ;
46+ float textTimer = 0.0f ;
47+ // The length of time text is offset
48+ const float animMaxTime = 0.1f ;
49+ float pasteAnim = 0.0f ;
50+ float copyAnim = 0.0f ;
51+ int copyAnimMult = 1 ;
52+ float textAnim = 0.0f ;
53+ float textAlpha = 0.0f ;
54+ // Offset amount for animations
55+ const int offsetAmount = -4 ;
56+ //--------------------------------------------------------------------------------------
57+
58+ // Main game loop
59+ while (!WindowShouldClose ()) // Detect window close button or ESC key
60+ {
61+ // Update
62+ //----------------------------------------------------------------------------------
63+ // Check if the user has pressed the copy/paste key combinations
64+ bool pastePressed = (IsKeyDown (KEY_LEFT_CONTROL ) && IsKeyPressed (KEY_V ));
65+ bool copyPressed = (IsKeyDown (KEY_LEFT_CONTROL ) && IsKeyPressed (KEY_C ));
66+
67+ // Update animation timers
68+ if (textTimer > 0 ) textTimer -= GetFrameTime ();
69+ if (pasteAnim > 0 ) pasteAnim -= GetFrameTime ();
70+ if (copyAnim > 0 ) copyAnim -= GetFrameTime ();
71+ if (textAnim > 0 ) textAnim -= GetFrameTime ();
72+
73+ // React to the user pressing paste
74+ if (pastePressed )
75+ {
76+ // Most operating systems hide this information until the user presses Ctrl-V on the window.
77+
78+ // Check to see if the clipboard contains an image
79+ // This function does nothing outside of Windows, as it directly calls the Windows API
80+ Image image = GetClipboardImage ();
81+
82+ if (IsImageValid (image ))
83+ {
84+ // Unload the image
85+ UnloadImage (image );
86+ // Update visuals
87+ popupText = "clipboard contains image" ;
88+ }
89+ else
90+ {
91+ // Get text from the user's clipboard
92+ clipboardText = GetClipboardText ();
93+
94+ // Update visuals
95+ popupText = "text pasted" ;
96+ pasteAnim = animMaxTime ;
97+ }
98+
99+ // Reset animation values
100+ textTimer = maxTime ;
101+ textAnim = animMaxTime ;
102+ textAlpha = 1 ;
103+ }
104+
105+ // React to the user pressing copy
106+ if (copyPressed )
107+ {
108+ // Set the text on the user's clipboard
109+ SetClipboardText (copyableText [textIndex ]);
110+
111+ // Reset values
112+ textTimer = maxTime ;
113+ textAnim = animMaxTime ;
114+ copyAnim = animMaxTime ;
115+ copyAnimMult = 1 ;
116+ textAlpha = 1 ;
117+ // Update the text that pops up at the bottom of the screen
118+ popupText = "text copied" ;
119+ }
120+
121+ // Switch to the next item in the list when the user presses up
122+ if (IsKeyPressed (KEY_UP ))
123+ {
124+ // Reset animation
125+ copyAnim = animMaxTime ;
126+ copyAnimMult = 1 ;
127+
128+ textIndex += 1 ;
129+
130+ if (textIndex >= sizeof (copyableText ) / sizeof (const char * )) // Length of array
131+ {
132+ // Loop back to the other end
133+ textIndex = 0 ;
134+ }
135+ }
136+
137+ // Switch to the previous item in the list when the user presses down
138+ if (IsKeyPressed (KEY_DOWN ))
139+ {
140+ // Reset animation
141+ copyAnim = animMaxTime ;
142+ copyAnimMult = -1 ;
143+
144+ if (textIndex == 0 )
145+ {
146+ // Loop back to the other end
147+ textIndex = (sizeof (copyableText ) / sizeof (const char * )) - 1 ; // Length of array minus one
148+ }
149+ else
150+ {
151+ textIndex -= 1 ;
152+ }
153+ }
154+ //----------------------------------------------------------------------------------
155+
156+ // Draw
157+ //----------------------------------------------------------------------------------
158+ BeginDrawing ();
159+
160+ ClearBackground (RAYWHITE );
161+
162+ // Draw the user's pasted text, if there is any yet
163+ if (clipboardText )
164+ {
165+ // Offset animation
166+ int offset = 0 ;
167+ if (pasteAnim > 0 ) offset = offsetAmount ;
168+
169+ // Draw the pasted text
170+ DrawText ("pasted clipboard:" , 10 , 10 + offset , 20 , DARKGREEN );
171+ DrawText (clipboardText , 10 , 30 + offset , 20 , DARKGRAY );
172+ }
173+
174+ // Offset animation
175+ int textOffset = 0 ;
176+ if (copyAnim > 0 ) textOffset = offsetAmount ;
177+
178+ // Draw copyable text and controls
179+ DrawText (copyableText [textIndex ], 10 , 330 + (textOffset * copyAnimMult ), 20 , MAROON );
180+ DrawText ("up/down to change string, ctrl-c to copy, ctrl-v to paste" , 10 , 355 , 20 , DARKGRAY );
181+
182+ // Alpha / Offset animation
183+ if (textAlpha > 0 )
184+ {
185+ // Offset animation
186+ int offset = 0 ;
187+ if (textAnim > 0 ) offset = offsetAmount ;
188+ // Draw pop up text
189+ DrawText (popupText , 10 , 425 + offset , 20 , ColorAlpha (DARKGREEN , textAlpha ));
190+
191+ // Fade-out animation
192+ if (textTimer < 0 )
193+ {
194+ textAlpha -= GetFrameTime ();
195+ }
196+ }
197+
198+ EndDrawing ();
199+ //----------------------------------------------------------------------------------
200+ }
201+
202+ // De-Initialization
203+ //--------------------------------------------------------------------------------------
204+ CloseWindow (); // Close window and OpenGL context
205+ //--------------------------------------------------------------------------------------
206+
207+ return 0 ;
208+ }
0 commit comments