Recent

Author Topic: Copy to and from clipboard works on Windows and not on MacOS  (Read 219 times)

daniel_sap

  • Jr. Member
  • **
  • Posts: 92
Copy to and from clipboard works on Windows and not on MacOS
« on: March 13, 2025, 02:05:24 pm »
Hi
I have this code which copies stream data to and from clipboard.
It works fine on Windows, but not under MacOS.

Code: Pascal  [Select][+][-]
  1. procedure CopyStreamToClipboard(fmt: Cardinal; S: TStream);
  2. begin
  3.   Clipboard.Open;
  4.   try
  5.     Clipboard.AddFormat(fmt, S); // Директно използване на fmt за Windows
  6.   finally
  7.     Clipboard.Close;
  8.   end;
  9. end;
  10.  

Code: Pascal  [Select][+][-]
  1. function copyClipboardToStream(aFormat: Cardinal; aStream: TStream): Boolean;
  2. begin
  3.   Result := false;
  4.  
  5.   if not Assigned(aStream) then
  6.     exit;
  7.  
  8.   Clipboard.Open();
  9.   try
  10.     if not Clipboard.HasFormat(aFormat) then
  11.       exit;
  12.  
  13.     Result := Clipboard.GetFormat(aFormat, aStream);
  14.   finally
  15.     Clipboard.Close();
  16.   end;
  17.  
  18. end;
  19.  

Can you, advice, what will be the appropriate approach to handle the stream read write to clipboard on MacOS

Thanks

Lazarus 4.0RC1 (rev t-fixes-4-58-g43d904ed50) FPC 3.2.2 x86_64-win64-win32/win64
Lazarus 4.99 (rev main_4_99-590-g9b726c777f) FPC 3.2.2 aarch64-darwin-coca

daniel_sap

  • Jr. Member
  • **
  • Posts: 92
Re: Copy to and from clipboard works on Windows and not on MacOS
« Reply #1 on: March 14, 2025, 05:20:19 pm »
The issue was that I pass Cardinal and it needs to be TClipboardFormat.
This code was ported from Delphi and not adjusted after that.

So, the copy and paste works perfect on Windows and MacOs

Here is the changed code, if someone came to such situation

Code: Pascal  [Select][+][-]
  1. function CopyStreamToClipboard(aFormat: TClipboardFormat; S: TStream): Boolean;
  2. begin
  3.   Clipboard.Open;
  4.   try
  5.     Result := Clipboard.AddFormat(aFormat, S);
  6.   finally
  7.     Clipboard.Close;
  8.   end;
  9. end;
  10.  
  11. function CopyClipboardToStream(aFormat: TClipboardFormat; aStream: TStream): Boolean;
  12. begin
  13.   Result := false;
  14.  
  15.   if not Assigned(aStream) then
  16.     exit;
  17.  
  18.   Clipboard.Open();
  19.   try
  20.     if not Clipboard.HasFormat(aFormat) then
  21.       exit;
  22.  
  23.     Result := Clipboard.GetFormat(aFormat, aStream);
  24.   finally
  25.     Clipboard.Close();
  26.   end;
  27.  
  28. end;
  29.  

 

TinyPortal © 2005-2018