0 0 Votes

Delphi/Lazarus: Prevent user from using CTRL+C in Memo or Edit Field

Tutorial by Delphian | 06/11/2013 at 02:00

Sometimes you do not want that a user is able to copy texts from a TMemo or TEdit control to the clipboard. That is, if the user selects the text from an Edit field or some lines from a Memo and then presses CTRL and C on his keyboard, the text should not be copied to the clipboard.

With the following code in the OnKeyUp event of the Memo or the Edit, we can just do that:

uses ClipBrd;
 
procedure Edit1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
begin    
  if (ssCtrl in Shift) and (Key = ord('C')) then begin
    Clipboard.AsText:=''; 
    // or Clipboard.AsText:='DO NOT COPY FROM US!';  
  end;
end;

The principle is as follows: First, we check which keys were pressed. If that is CTRL (Shift) and C (Key) at the same time, we set the clipboard content to an empty string. Thus, it is not possible for the user to copy a content to the clipboard.

Of course, instead of an empty string, we can also copy any other alternative text to the clipboard as the uncommented line shows.

ReplyPositiveNegative

About the Author

Avatar AuthorThe author has not added a short description to his profile yet.
Show Profile | Message

 

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 articles on askingbox.com. How to do.