0
votes

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?

3

3 Answers

13
votes

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.

3
votes

OnKeyDown gives you a scancode. OnKeyPress gives you the character. Been that way in every version of Delphi I can remember.

0
votes

to send Key:=0; on KeyDown Event use:

Key:=HiWord(GetKeyState(0));

//do something