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

Comments

Adren Software
(Posted 23 years ago)
Does this work with Blitz3D?

Jim Brown
(Posted 23 years ago)
Yes.

DjBigWorm
(Posted 23 years ago)
Hi,

I tried your Clipboard - Text Copy & Paste.

I found a problem

in the declaration you used
;GetClipboardData$(format%):"GetClipboardData"
But, in the function you called
SetClipboardText cb_TEXT,txt$

I just changed the SetClipboardText to SetClipboardData
as well as GetClipboardText$(cb_TEXT)
should use the word data instead of text.

That is all, thought to let you know so that you could change it. Great functions!!!

Wayne
(Posted 21 years ago)
Junk! Doesn't work.

mongia2
(Posted 20 years ago)
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
SetClipboarddata cb_TEXT,txt$
CloseClipboard
EndIf
End Function

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

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

BlackD
(Posted 19 years ago)
The code above I've had trouble rolling out across multiple systems. There seem to be incompatiablities with Vista in some cases where users are not running as administrator and have separate user-space thingees turned on. :P (Even changing the DECLS and code to work off a bank instead.. it'd work on one system them not another).

For a quick and easy way out, I grabbed the clipboard access DLL from here: http://www.icynorth.com/development/clipboard_dll.html
(Public domain release: "You are free to use this DLL in any way that you wish without restrictions." from website, although I'd recommend attribution to Brett Kapilik.)

After renaming the dll to "clipboard.dll" purely for testing, I created this clipboard.decls file:

.lib "clipboard.dll"
IsTextOnClipboard%():"IsTextOnClipboard"
CopyTextToClipboard%(strText$):"CopyTextToClipboard"
GetTextFromClipboard$():"GetTextFromClipboard"


And some sample code:

If IsTextOnClipboard() Then 
	Print "Text already on the clipboard.."
	Print "but we're going To wipe it anyway..":Print
	End If

SeedRnd MilliSecs()
a$="**"+Str$(Rand(0,1000))+"**"
Print "Writing "+a$+" to the clipboard.."
CopyTextToClipboard a$

b$=GetTextFromClipboard$()
Print "Read "+b$+" from the clipboard.."

Print:If a$=b$ then print "SUCCESS!"

WaitKey()
End


Unfortunately the DLL doesn't come with source so I have no idea if it's closing the reads and writes cleanly, though tracing its behaviour and using it extensively it appears to do exactly what it says on the box.

+BlackD

D4NM4N
(Posted 18 years ago)
THANK YOU BlackD!!!!!

You just saved my life!
http://www.blitzbasic.com/Community/posts.php?topic=78874

(I think, more testing is needed but seems to work fine :)

Code Archives Forum