Monitor the clipboard
Re: Monitor the clipboard
The monitor code based on such clipboard chain works poorly in Win7 (I have one my old program ported from XP, it cannot work stable for a lot of days as it worked on XP, for some reason it is sometimes dropped from viewers chain by that new rewritten stupid Windows subsystem, and it is so hard to track this). There is another way from new windows APIs, but complicated enough and didn't tried yet, anyway it should be used on Vista and higher instead of this
"W̷i̷s̷h̷i̷n̷g o̷n a s̷t̷a̷r"
Re: Monitor the clipboard
I was just trying Rashad's code here -> https://www.purebasic.fr/english/viewto ... 69#p480169
But it doesn't work anymore. It fails with the PeekS() line, as shown below. Yes, I've copied some rich text to the clipboard before running it.
Rashad, are you able to update it, pretty please? Hehe.
But it doesn't work anymore. It fails with the PeekS() line, as shown below. Yes, I've copied some rich text to the clipboard before running it.
Rashad, are you able to update it, pretty please? Hehe.
Code: Select all
CF_RTF=RegisterClipboardFormat_("Rich Text Format")
If OpenClipboard_(0)
cbHnd = GetClipboardData_(CF_RTF)
txtLen = lstrlen_(cbHnd)*SizeOf(character)
cliprtf$ = Space(txtLen)
CopyMemory_( cliprtf$, cbHnd, txtLen)
cliprtf$ = PeekS(@cliprtf$,txtLen,#PB_UTF8) ; <- FAILS :(
Debug cliprt$ ; Returns an empty string due to PeekS() failing.
cliprtf$=ReplaceString(cliprtf$,"PureBasic","PUREBASIC")
mem = GlobalAlloc_(#GHND,lstrlen_(cliprtf$)*SizeOf(character)+1)
lpstr = GlobalLock_(mem)
PokeS(lpstr,cliprtf$,-1,#PB_UTF8)
GlobalUnlock_(mem)
EmptyClipboard_()
SetClipboardData_(CF_RTF,mem)
CloseClipboard_()
GlobalFree_(mem)
EndIf
Re: Monitor the clipboard
Hi BarryG
Works as expected just it is typo mistake
Works as expected just it is typo mistake
Code: Select all
;Debug cliprt$ ; Returns an empty string due to PeekS() failing.
Debug cliprtf$
Egypt my love
Re: Monitor the clipboard
The code works for me, but only if a text is stored in the clipboard at the start of the program.
Otherwise the handle of GetClipboardData_(CF_RTF) is already NULL.
BTW: With PB v600 the EditorGadget() can also directly read the RTF text correctly with Ctrl+V.
Otherwise the handle of GetClipboardData_(CF_RTF) is already NULL.
BTW: With PB v600 the EditorGadget() can also directly read the RTF text correctly with Ctrl+V.
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
Re: Monitor the clipboard
Works as expected just it is typo mistake
EnableExplicit
is your best friend, even in the smallest examples.

Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
Re: Monitor the clipboard
Hmm, okay that incorrect variable name was just a one-off typo when I posted the question. My bad. But that's not the problem.
Debugging with the correct variable name still shows that the clipboard text wasn't copied to it with PeekS() - it returns an empty string. Look at the debug output below after I copied the rich text from WordPad and ran the code. You can clearly see the variable name is correct (it's highlighted when selected in the IDE), and that I've copied the rich text (it's showing in the Windows Clipboard history).
So, ignore my typo: the code below still isn't working.

Debugging with the correct variable name still shows that the clipboard text wasn't copied to it with PeekS() - it returns an empty string. Look at the debug output below after I copied the rich text from WordPad and ran the code. You can clearly see the variable name is correct (it's highlighted when selected in the IDE), and that I've copied the rich text (it's showing in the Windows Clipboard history).
So, ignore my typo: the code below still isn't working.

Re: Monitor the clipboard
Tested with PB 5.73 x86 - x64 Windows 11 x64
Works like a charm
What is your configuration ?
Works like a charm
What is your configuration ?
Egypt my love
Re: Monitor the clipboard
Try the next snippet it's less headache
Code: Select all
CF_RTF=RegisterClipboardFormat_("Rich Text Format")
If OpenClipboard_(0)
hMem = GetClipboardData_(CF_RTF)
If hMem
txtLen = GlobalSize_(hMem)
src = GlobalLock_(hMem)
If src
dest = AllocateMemory(txtLen )
If dest
CopyMemory(src,dest,txtLen)
Debug PeekS(dest,txtLen,#PB_UTF8)
EndIf
GlobalUnlock_(hMem)
EndIf
EndIf
CloseClipboard_()
EndIf
Egypt my love
Re: Monitor the clipboard
Win 10 Pro (64-bit) with PureBasic 6.00 LTS (x64). Here's a video -> https://i.imgur.com/AdJUSdW.mp4
Your second example doesn't show anything in the Debug Output, either. Probably a PureBasic bug?
Re: Monitor the clipboard
I tried this code and with text copied from wordpad returns this in the output:
And no difference between ASM and C backend (v600 on Win10 Home)
Code: Select all
; EnableExplicit
CF_RTF = RegisterClipboardFormat_("Rich Text Format")
Debug CF_RTF
If OpenClipboard_(0)
hMem = GetClipboardData_(CF_RTF)
If hMem
Debug hMem
txtLen = GlobalSize_(hMem)
src = GlobalLock_(hMem)
If src
Debug src
dest = AllocateMemory(txtLen )
If dest
Debug dest
CopyMemory(src,dest,txtLen)
Debug ">> " + PeekS(dest,txtLen,#PB_UTF8)
EndIf
GlobalUnlock_(hMem)
EndIf
EndIf
CloseClipboard_()
EndIf
; my output with clipboard rich text
; 49292
; 4416848
; 4416848
; 77269088
; >> {\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1031{\fonttbl{\f0\fnil\fcharset0 Calibri;}}
; {\*\generator Riched20 10.0.19041}\viewkind4\uc1
; \pard\sa200\sl276\slmult1\f0\fs22\lang7 the word \b purebasic\b0 is bold \par
; }
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
Re: Monitor the clipboard
Thanks to Axolotl's code, I worked out the problem. Seems you need to have something added to the string BEFORE what PeekS() creates for that string.
These first two examples work and show the expected output:
But these two don't work and show nothing in the Debug Output window:
Is this a PureBasic bug, maybe?
These first two examples work and show the expected output:
Code: Select all
Debug ">> " + PeekS(dest,txtLen,#PB_UTF8) ; Shows expected output
Code: Select all
text$ = ">> " + PeekS(dest,txtLen,#PB_UTF8)
Debug text$ ; Shows expected output
Code: Select all
Debug PeekS(dest,txtLen,#PB_UTF8) ; Shows nothing
Code: Select all
text$ = PeekS(dest,txtLen,#PB_UTF8)
Debug text$ ; Shows nothing
Re: Monitor the clipboard
That's weird.
I confirm your observation with #PB_Compiler_Backend = #PB_Backend_C only once. (I probably made a mistake in the test steps.)
I can change my code to
BTW: I use the automatically convertion from integer to string if there is a string at the beginning. that's why I like to use a "" at the beginning in debug. For many outputs debug "VarName = " + VarName is also quite helpful.
I confirm your observation with #PB_Compiler_Backend = #PB_Backend_C only once. (I probably made a mistake in the test steps.)
I can change my code to
Code: Select all
Debug PeekS(dest,txtLen,#PB_UTF8) ; Shows expected output -- worked on both backends at my computer ????
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).