Clipboard data

BlitzMax Forums/BlitzMax Programming/Clipboard data

VP(Posted 2006) [#1]
Is it at all possible to read and write data from the clipboard without the gui module?

I just need to enable cut/copy/paste from within a windowed app under Windows and Linux.

TIA


EOF(Posted 2006) [#2]
Windows version:
format_codebox('' Clipboard Text - Copy/Paste functions

Strict

Extern "Win32"
Function OpenClipboard%(hwnd%)
Function CloseClipboard%()
Function EmptyClipboard%()
Function IsClipboardFormatAvailable%(format%)
Function GetClipboardData:Byte Ptr(Format:Int)
Function SetClipboardData(format%, hMem:Byte Ptr)
Function GlobalAlloc(Flags:Int, Bytes:Int)
Function GlobalFree(Mem:Int)
Function GlobalLock:Byte Ptr(Mem:Int)
Function GlobalUnlock(Mem:Int)
End Extern

' -----------------------------------------------

Function TextFromClipboard:String()
Const CF_TEXT%=$1
If Not OpenClipboard(0) Return ""
Local TextBuf:Byte Ptr = GetClipboardData(CF_TEXT)
CloseClipboard()
Return String.FromCString(TextBuf)
End Function

Function TextToClipboard(txt:String)
Const CF_TEXT%=$1
Const GMEM_MOVEABLE%=$2
Const GMEM_DDESHARE%=$2000
If txt$="" Return
Local TextBuf:Byte Ptr = Txt.ToCString()
Local Memblock:Int = GlobalAlloc(GMEM_MOVEABLE|GMEM_DDESHARE, txt.Length+1)
Local DataBuf:Byte Ptr = GlobalLock(Memblock)
MemCopy DataBuf, TextBuf, Txt.length
If OpenClipboard(0)
EmptyClipboard
SetClipboardData CF_TEXT, DataBuf
CloseClipboard
EndIf
GlobalUnlock Memblock
GlobalFree Memblock
End Function


' ********************************************


' test

Print "Clipboard Test."
Print "==============="
Print "Enter a message for the clipboard."
Print "Alternatively, leave BLANK to read clipboard."

Local a:String

a=Input$(">")

If a=""
a=TextFromClipboard()
Print a
Else
TextToClipboard a
Print "Text sent to clipboard. Open NotePad and paste!"
EndIf

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

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

End')


Fielder(Posted 2017) [#3]
Hi Jim, i've tried your code and i have some issues trying to add a line feed / CR to the string.. (i need to write 3 lines on notepad using clipboard)

i've tried adding chr(13) ...

after your suggestion on forum i used chr(10)

and again i've only a single line on clipboard paste :(

but after reading some similar issues on web i found that is necessary to add this:

+chr(13)+chr(10)

P.S. thank you for your code Jim , you saved my day