Recent

Author Topic: How do i grab text from clipboard?  (Read 4446 times)

Juanma

  • Newbie
  • Posts: 1
How do i grab text from clipboard?
« on: March 27, 2018, 08:28:34 pm »
I was trying to write a small program that grabs text from clipboard then assign it to a string var.

i'm using  Free Pascal Compiler version 2.6.2-8   on Ubuntu 14.04LTS. 

i did quite a lot of google searches and this link http://wiki.lazarus.freepascal.org/Clipboard was the first thing i saw. it says
Code: Pascal  [Select][+][-]
  1. Clipboard is a TClipboard variable and the Clipbrd unit should be added in the uses clause in order to use the variable:
which i did but i'm not sure what i am doing wrong, it says that unit clipbrd doesn't exist. 

Code: Pascal  [Select][+][-]
  1. program test;
  2. uses
  3.            Clipbrd;
  4.  
  5. .....

Code: Pascal  [Select][+][-]
  1. Compiling test.pas
  2. test.pas(3,5) Fatal: Can't find unit Clipbrd used by test
  3. Fatal: Compilation aborted
  4.  

thanks

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: How do i grab text from clipboard?
« Reply #1 on: March 27, 2018, 08:34:00 pm »
Code: Pascal  [Select][+][-]
  1. uses
  2.   clipbrd;
  3. var
  4.   s: String;
  5. begin
  6.   s := Clipboard.AsText;

https://lazplanet.blogspot.de/2013/05/how-to-cut-copy-paste-text-in-your.html

This works in GUI programs. I don't think that you can access the clipbpard in a console program (at least not in this way).
« Last Edit: March 27, 2018, 08:36:09 pm by wp »

furious programming

  • Hero Member
  • *****
  • Posts: 858
Re: How do i grab text from clipboard?
« Reply #2 on: March 30, 2018, 12:29:03 am »
@wp: it is possible to use Clipboard object in console application.

First of all, we need to add LCL package in Project Inspector window (node Required Packages), to get the access to the Clipboard object. Then we need to add Clipbrd to the uses list:

Code: Pascal  [Select][+][-]
  1. program ClipboardTest;
  2.  
  3. {$MODE OBJFPC}{$LONGSTRINGS ON}
  4.  
  5. uses
  6.   Clipbrd;
  7. begin
  8.   Write('Clipboard content: ', Clipboard.AsText);
  9. end.

Boom, exception – TClipboard use a widgetset, so it must be created. This is done in the initialization section of the unit Interfaces, so we need to add this unit to the list (as first):

Code: Pascal  [Select][+][-]
  1. program ClipboardTest;
  2.  
  3. {$MODE OBJFPC}{$LONGSTRINGS ON}
  4.  
  5. uses
  6.   Interfaces, Clipbrd;
  7. begin
  8.   Write('Clipboard content: ', Clipboard.AsText);
  9. end.

Works correctly for me (at least on the Windows).
« Last Edit: March 30, 2018, 12:32:07 am by furious programming »
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

 

TinyPortal © 2005-2018