00 Votes

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

Tutorial by Delphian | 2013-11-06 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);
  if (ssCtrl in Shift) and (Key = ord('C')) then begin
    // or Clipboard.AsText:='DO NOT COPY FROM US!';  

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.


About the Author

AvatarThe author has not added a profile short description yet.
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.


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