Code archives/Miscellaneous/Clipboard - Text Copy & Paste

This code has been declared by its author to be Public Domain code.

Download source code

Clipboard - Text Copy & Paste by Jim Brown
(Posted 24 years ago)
A couple of functions for reading and writing text to and from the clipboard.

a$=ReadClipboardText()
Opens the clipboard and passes any available text into a$.

WriteClipboardText "Hello World!"
Opens the clipboard and sends the message there clearing any previous text as it goes.

Credits to the following coders:
Ed from Mars - for clearing up a couple of userlib declarations.
Darkuni - for tracking down potentional collisons when accessing the clipboard.

NOTE:
=====
You need to save a 'user32.decls' file into the userlibs path. See the declarations in the code.
; Clipboard Text Read / Write
; ===========================
; Syntax Error & Ed from Mars


; userlib declarations - 'user32.decls'
; *********************************************
; .lib "user32.dll"
; OpenClipboard%(hwnd%):"OpenClipboard"
; CloseClipboard%():"CloseClipboard"
; ExamineClipboard%(format%):"IsClipboardFormatAvailable"
; EmptyClipboard%():"EmptyClipboard"
; GetClipboardData$(format%):"GetClipboardData"
; SetClipboardData%(format%,txt$):"SetClipboardData"
; *********************************************


Print "Clipboard Test." 
Print "~~~~~~~~~~~~~~~" 
Print "Enter a message for the clipboard." 
Print "Alternatively, leave BLANK to read clipboard." 
a$=Input$(">") 

If a$="" 
	a$=ReadClipboardText$() 
	Print a$
Else 
	WriteClipboardText a$ 
	Print "Text sent to clipboard. Open NotePad and paste!"
EndIf

Print Chr$(13)+"---------------------------------"

a$=Input$("Press RETURN to end ...") 

End

;-----------------------------------

Function WriteClipboardText(txt$)
	Local cb_TEXT=1
	If txt$="" Then Return 
	If OpenClipboard(0)
		EmptyClipboard
		SetClipboardText cb_TEXT,txt$
		CloseClipboard
	EndIf
End Function

;-----------------------------------

Function ReadClipboardText$()
	Local cb_TEXT=1
	Local txt$=""
	If OpenClipboard(0)
		If ExamineClipboard(cb_TEXT) 
			txt$=GetClipboardText$(cb_TEXT)
		EndIf
		CloseClipboard
	EndIf
	Return txt$
End Function