Question

I would like to prevent copy, cut and paste in my TEdit. How can I do this?

I tried setting the Key=NULL on KeyDown event when CTRL+V was pressed on the control, but it didn't work.

Was it helpful?

Solution

You'll need to prevent the WM_CUT, WM_COPY, and WM_PASTE messages from being sent to your TEdit. This answer describes how do to this using just the Windows API. For the VCL, it may be sufficient to subclass TEdit and change its DefWndProc property or override its WndProc method.

OTHER TIPS

Assign this to TEdit.OnKeyPress :

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
 if (Key=#22) or (Key=#3) then Key:=#0;   // 22 = [Ctrl+V] / 3 = [Ctrl+C]
end;

I know this is an old question but I'll add what I have found. The original poster almost had the solution. It works fine if you ignore cut/copy/paste in the key press event instead of the key down event. ie (c++ builder)

void __fastcall Form::OnKeyPress(TObject *Sender, System::WideChar &Key)
{
   if( Key==0x03/*ctrl-c*/ || Key==0x16/*ctrl-v*/ || Key==0x018/*ctrl-x*/ )
      Key = 0;  //ignore key press
}

You can use some global programs that grab shortcuts and block C-V C-C C-X when TEdit window is active

Uses Clipbrd;

procedure TForm1.Edit1Enter(Sender: TObject);
begin
  Clipboard.AsText := '';
end;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top