Title: Disable (Intercept) CTRL+V ("Paste") for a TEdit or TMemo Delphi controls
Sometimes you need to disallow a user from using keyboard shortcuts for common clipboard operations such as copy (CTRL+C) or paste (CTRL+V) while an edit control has the input focus.
Imagine a situation where you have two edit boxes "designed" to let the user change the password for your database application.
The user needs to enter the new password two times. The first edit box is where the user specifies the new password and the second edit box is used to let the user confirm the new password.
You might want to ensure that a password typed in the first edit control is not copied to the second one - but rather you want to make sure it gets manually typed in the "confirm password" edit box.
No "Paste" for you!
To intercept any key combination for a TEdit (or TMemo or more generally TCustomEdit) you need to handle the OnKeyDown event.
Put a TEdit named "Edit1" on a form (named "Form1"). Handle Edit1's OnKeyDown event as:
uses Clipbrd, ...
//disable CTRL + V ("Paste") :: handles Edit1.OnKeyDown
procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState) ;
begin
if ((ssCtrl in Shift) AND (Key = ord('V'))) then
begin
if Clipboard.HasFormat(CF_TEXT) then ClipBoard.Clear;
Edit1.SelText := '"Paste" DISABLED!';
Key := 0;
end;
end;
When the user presses the CTRL+V key combination while Edit1 has the input focus ... the code above will clear the clipboard and "paste" the '"Paste" DISABLED!' text into the edit control.