Read and write clipboard (Linux and Windows)

General FreeBASIC programming questions.
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Read and write clipboard (Linux and Windows)

Post by deltarho[1859] »

@Josep Roca

I know. For those that have WinFBE check out 'Help>WinFBX Framework Help>WinFBX Library>Windows Procedures' and use 'Find' to find AfxSetClipboardText. There you will find four other procedures, and they are as easy to use as PowerBASIC and just as informative.

Example:

Code: Select all

#Include Once "Afx\CWindow.inc"
Using Afx
Dim ClipResult As Handle
Do
  ClipResult = AfxSetClipboardText("FreeBASIC")
Loop Until ClipResult <> Null

Print AfxGetClipboardText

Sleep
Added: I noticed that I used CWindow.inc. That includes AfxWin.inc. I use WinFBX for writing GUIs.
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: Read and write clipboard (Linux and Windows)

Post by caseih »

deltarho[1859] wrote:Agreed. PowerBASIC has nine clipboard keywords, Windows based, of course.

Code: Select all

Do
  Clipboard Set Text ClipContents, ClipResult
Loop Until IsTrue ClipResult
I agree that most languages including FB should offer clipboard functionality, but it should only be in the runtime library as a normal, callable, function, and not as yet another keyword with a special syntax.

I don't care at all for the PowerBASIC syntax you show here either. Come to that I don't care for a lot of BASIC special syntax such as the way LINE, PSET, etc work.
Josep Roca
Posts: 564
Joined: Sep 27, 2016 18:20
Location: Valencia, Spain

Re: Read and write clipboard (Linux and Windows)

Post by Josep Roca »

Code: Select all

Dim ClipResult As Handle
Do
  ClipResult = AfxSetClipboardText("FreeBASIC")
Loop Until ClipResult <> Null
A tip to make it more compact:

Code: Select all

Do
Loop Until AfxSetClipboardText("FreeBASIC") <> Null
paul doe
Moderator
Posts: 1730
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: Read and write clipboard (Linux and Windows)

Post by paul doe »

A few interesting links, courtesy of coderJeff:

viewtopic.php?t=3072
viewtopic.php?t=4635
viewtopic.php?t=5554
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Read and write clipboard (Linux and Windows)

Post by deltarho[1859] »

@Josep Roca

Thanks, José.

Even more compact:

Code: Select all

Do : Loop Until AfxSetClipboardText("FreeBASIC") <> Null
Image
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Read and write clipboard (Linux and Windows)

Post by deltarho[1859] »

... and if we set the clipboard text a few times, how's about:

Code: Select all

#define SetClip(Text) Do : Loop Until AfxSetClipboardText(Text) <> Null
#define Clip AfxGetClipboardText
...
SetClip("FreeBASIC")
Print Clip
using jj2007's syntax.

Added: Needless to say, we can do something similar with José's three other procedures and then create Clipboard.bi

Don't forget, we are talking Windows here and that 'Text' is Unicode aware.
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Read and write clipboard (Linux and Windows)

Post by dodicat »

I get access denied with powershell Set-clipboard. so I revert to dos for setclipboard...

Code: Select all


Sub setclipboard(s As String)
      Dim As String f="tmpclip.dat"
      Dim As Long n=Freefile
      If Open (f For Binary Access Write As #n)=0 Then
            Put #n,,s
            Close
      Shell "clip < "+f
      Kill f
      Else:Print "FAIL":End If
End Sub

Function getclipboard() As String
      Dim As String f="tmpclip.dat",text
      Shell  "powershell Get-Clipboard -raw > "+f
      Dim As Long n=Freefile
      If Open (f For Binary Access Read As #n)=0 Then
            If Lof(n) > 0 Then
                  text = String(Lof(n),0)
                  Get #n, , text
            End If
            Close
            Kill f
            Return text
      Else:Print "FAIL":End If
End Function


setclipboard(Command(0)+Chr(10)+" Press any key to end . . .")
Print getclipboard()
Sleep
 
Feel free to comment or fix if riddled.
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Read and write clipboard (Linux and Windows)

Post by deltarho[1859] »

dodicat wrote:I get access denied with powershell Set-clipboard.
Post the code that fails.
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Read and write clipboard (Linux and Windows)

Post by deltarho[1859] »

Append and Clear

Code: Select all

#Include Once "Afx\CWindow.inc"
#define CrLf Chr(10) + Chr(13)
#define SetClip(Text) Do : Loop Until AfxSetClipboardText(Text) <> Null
#define SetClipAppend(Text) Do : Loop Until AfxSetClipboardText(Clip+CrLf+Text) <> Null
#define Clip AfxGetClipboardText
#define ClipClr Do : Loop Until AfxClearClipboard <> 0

Using Afx
SetClip("FreeBASIC")
SetClipAppend("Another line")
SetClipAppend("...and another line")
Print Clip

Sleep
Output:

Code: Select all

FreeBASIC
Another line
...and another line
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Read and write clipboard (Linux and Windows)

Post by dodicat »

powershell Set-Clipboard -Value somestring doesn't accept spaces, even if somestring is in quotes.
Which is a bit duff.
I don't think powershell has reached it's capacity yet (it is relatively new)
I am working on the space thing.
But the DOS clip has been around since ..er.. DOS days, I suppose win 10 uses a similar method in it's DOS simulation.
Shell "clip < "+f seems to add a full carriage return to that which is to be put on the clipboard, which has to be skimmed off ASAP.
Here are append and prepend clipboard self contained.

Code: Select all


Enum
      _new
      _append
      _prepend
End Enum

Function loadfile(file As String) As String
      Dim As Long  f=Freefile
      if Open (file For Binary Access Read As #f)=0 then
      Dim As String text
      If Lof(f) > 0 Then
            text = String(Lof(f), 0)
            Get #f, , text
      End If
      Close #f
      Return text
      else:print file+" not found":end if
End Function

Function getclipboard() As String
      Dim As String f="tmpclip.dat"
      Shell  "powershell Get-Clipboard -raw > "+f
      Return Rtrim(loadfile(f),Chr(13,10))
      Kill f
End Function

Sub savefile(filename As String,p As String)
      Dim As Long n=Freefile
      If Open (filename For Binary Access Write As #n)=0 Then
            Put #n,,p
            Close
      Else:Print "Unable to save " + filename:end if
End Sub

Sub pre_pend(filename As String,txt As String) 
      If Len( getclipboard()) Then 
            savefile(filename,getclipboard())
      Else
            savefile(filename,"") 
      End If
      Dim As String s=loadfile(filename)
      savefile(filename,txt+s)
End Sub

Sub ap_pend(filename As String,txt As String)
      If Len( getclipboard()) Then 
            savefile(filename,getclipboard())
      Else
            savefile(filename,"") 
      End If
      Dim As String s=loadfile(filename)
      savefile(filename,s+txt)
End Sub

Sub setclipboard(s As String,flag As Integer=0)
      Dim As String f="tmpclip.dat"
      Select Case flag
      Case 0
            savefile(f,s)
      Case 1
            ap_pend(f,s)
      Case 2
            pre_pend(f,s)
      End Select
      Shell "clip < "+f
      Kill f
End Sub


setclipboard("Text on clipboard")
setclipboard(Chr(10)+" Press any key to end . . .",_append)
setclipboard("Brand new clipboard"+Chr(13,10),_prepend)
Print getclipboard()

Sleep 
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Read and write clipboard (Linux and Windows)

Post by deltarho[1859] »

Prepend? Strewth. Image

Code: Select all

#define SetClipPrepend(Text) Do : Loop Until AfxSetClipboardText(Text+CrLf+Clip) <> Null

Code: Select all

SetClip("Initial analysis")
...
SetClipPrepend("Further analysis")
...
SetClipPrepend("...even more analysis")
SetClipAppend(CrLf + "Job done")
Print Clip
Output:

Code: Select all

...even more analysis
Further analysis
Initial analysis

Job done
@dodicat

If you come up with an insert, then I am going fishing. Image
xlucas
Posts: 334
Joined: May 09, 2014 21:19
Location: Argentina

Re: Read and write clipboard (Linux and Windows)

Post by xlucas »

adeyblue wrote:Congratulations. You now have a third party library in your project that [...]
To this comment, the only adequate reply I could make is not allowed or correct in this forum, I suppose.

To the folks discussing about whether the Windows API should be considered a "third-party library", I pretty much agree with what you've been saying here. Bill Gates is not somebody I like very much and I'm sure many, many bugs exist in the API and it is third-party in the sense that I did not write it, but any other library I use will ultimately call the API, so the only way for me to minimise bugs and maximise code independence is to directly use the API. Besides, this "library" is included in the OS installation.
caseih wrote:But dodicat's code, which is no doubt fairly correct, is the very third-party code library that @xlucas was complaining about.
This, I deserve it. It is true. I could've perfectly gone and looked up the code within the library that Caseih posted. When I complained about third-party libraries, I really felt very bad at the prospect of, with my complain, throwing a negative to a fellow who was trying to help. I expressed this by pointing out it was today's reality I was complaining about. We're all on the same ship here.

The reason why Dodicat's code felt to more more immediately useful was because I could read in the code he posted the two or three lines that I wanted to put in mine. It turns out for some reason, it compiles perfectly well, but it's not doing anything on the clipboard when I try it on a windows machine. I'm sure the code must be correct. I must be doing something wrong. I'll take a better read at the code all of you guys have been posting and I'll get it to work or otherwise, I'll post here the details if I have any more problems. I have to go to bed now. Thank you all so much!
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Read and write clipboard (Linux and Windows)

Post by jj2007 »

The only adequate reply to adeyblue's comment is "thanks, you are right"
grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: Read and write clipboard (Linux and Windows)

Post by grindstone »

xlucas wrote:...but it's not doing anything on the clipboard when I try it on a windows machine...
Here (with WinXP and GIMP) it works. Once having run dodicat's snippet I can copy the picture (or is it a graphic?) to a GIMP sheet by pressing "Ctrl + v".

For reading/writing text I usually use these two procedures:

Code: Select all

#Include "windows.bi"

Sub WriteClipboard(Text As String)
	Dim As Any Ptr lpMem, hGlobalClip

	hGlobalClip = GlobalAlloc(GMEM_MOVEABLE Or GMEM_SHARE, Len(Text) + 1)
	OpenClipboard(NULL)
	EmptyClipboard()
	lpMem = GlobalLock(hGlobalClip)
	lstrcpy(lpMem, StrPtr(Text))
	GlobalUnlock(lpMem)
	SetClipboardData(CF_TEXT, hGlobalClip)
	CloseClipboard()
End Sub

Function ReadClipboard() As String
	Dim As Any Ptr hGlobal, pGlobal
	Dim As String txt = ""

	OpenClipboard(NULL)
	hGlobal = GetClipboardData(CF_TEXT)
	If hGlobal Then
		pGlobal = GlobalLock(hGlobal)
		txt = Space(lstrlen(pGlobal))
		lstrcpyA(StrPtr(txt), pGlobal)
	End If
	Function = txt
	CloseClipboard()
End Function

WriteClipboard("Write this text to the clipboard")
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Read and write clipboard (Linux and Windows)

Post by dodicat »

Thanks grindstone (and jj2007)
With my own windows code the text to clipboard should be tested at the sleep, by pasting what is there to your ide or notepad or whatever.
it should be
Please press any key to continue . . .
(Which maybe was a silly sentence to copy)
After sleep the bitmap is copied so nothing will appear in notepad or whatever, but will paste OK to windows paint.

With my powershell method it is only text anyway, I haven't yet experimented with pictures.
Win 10, fb 1.08.0
Post Reply