R – Delphi 2009 OnKeyDown does not trap as Delphi 2007 does

delphidelphi-2009keyboardkeydown

I have this code

procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
var
begin
  if not (Key in [Ord('0')..Ord('9')]) then
    Key := 0;
end;

and it worked fine with Delphi 2007. When I upgraded to Delphi 2009 and I try to press any letter it is accepted and the Key := 0 does not trap the input?

Anyone encountered the same behavior?

Best Answer

Are you sure that this worked in Delphi 2007? I just tried the code in Delphi 2007 and 2009. And both behave the same (No key stroke is eaten) If you want to accept only digits you should use the OnKeyPress event and set the Key parameter to #0.