33 Votes

Delphi/Lazarus: Catch CTRL+C in Memo or Edit and Change Clipboard Content

Tutorial by Stefan Trost | Last update on 2023-01-21 | Created on 2013-11-06

Whenever a user presses CTRL + C in a TMemo or TEdit control, normally the content of this memo or edit field is copied to the clipboard.

However, at this point, many manipulations are possible. Either we want to completely prevent that it is possible to copy any content from our program to the clipboard or we do not want to copy the (exact) content but something else. Either a completely different string or a supplement to the existing one. For example, this can be a unit copied behind a number from an edit field.

All these manipulations we can easily perform by applying the following code to the OnKeyUp-Event of our Memo or Edit (or whatever else control):

uses ClipBrd;

procedure Memo1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
begin    
  if (ssCtrl in Shift) and (Key = ord('C')) then begin

    // Option 1: Delete entire content
    Clipboard.AsText := ''; 

    // Option 2: Modify content
    Clipboard.AsText := 'Enjoy: ' + Memo1.Text + ' Copied from ...'; 
    Clipboard.AsText := StringReplace(Memo1.Text, 'old', 'new', [rfReplaceAll]); 
    Clipboard.AsText := Memo1.Text + ' kg';
    
    // Option 3: Provide another content
    Clipboard.AsText := 'Another Text.'; 

  end;
end;

In the OnKeyUp event, we first look at whether the keys CTRL and C are pressed simultaneously. If this is the case, we can next copy any text to the clipboard by simply changing respectively overwriting the property Clipboard.AsText, with which the current content of the clipboard can be set. So that we can use "Clipboard", we only have to include the unit "Clipbrd" into our "uses" section.

In the example, we show 3 ways how we can manipulate the content of the clipboard:

  • Delete content completely: To empty or clear the content of the clipboard, it is sufficiently to set "Clipboard.AsText" on an empty string.
  • Modify content: If we want to change the content of the clipboard, the usual string functions are available to us. As an example, we show three different edits: once we replace a word with another, once we hang a unit on the text and in the third example we embed the text of the memo into another text.
  • Provide other content: If we want to provide the user a completely different text that has nothing to do with the current text of the input field, we can of course also set the content of the clipboard to any other text.

Of course, we can alternatively also use any other oppurtunity of string manipulation or we can even set a completely different content instead of the text, such as copying a picture into the clipboard.

ReplyPositiveNegative

About the Author

AvatarYou can find Software by Stefan Trost on sttmedia.com. Do you need an individual software solution according to your needs? - sttmedia.com/contact
Show Profile

 

Related Topics

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.