Shagrouni

Using Enter Key

Khaled Shagrouni, December 23 2000

Arabic

Its hard to get users leave their old habits.

Delphi Papers

Published in Delphi3000.com
and in www.sourcer.ur (Russian),
some whwere (Chinees)

When ever I install a new application; I heard the users asking why Enter key doesn’t behave like Tab key? (Seems Windows didn’t change some habits inherited from the old text mode culture). So I come up with this routine, which can be planted inside the KeyDown event of the data entry form.

I am sure that many others made a better solution than mine; but some times doing the thing by your self is faster than searching for it elsewhere.

procedure TForm1.EditKeyPress(Sender: TObject; var Key: Char);
begin
  if key = #13 then key := #0;
end;

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
var
  ACtrl: TWinControl;
  k: TKeyPressEvent;
begin

  if key = 13 then
    begin
      ACtrl := ActiveControl;
      if ACtrl is TCustomMemo then exit;

      if ACtrl is TEdit then
        begin
          if assigned(TEdit(ACtrl).onKeyPress) then
            k:= TEdit(ACtrl).OnKeyPress;
          TEdit(ACtrl).OnKeyPress := EditKeyPress;
        end;

      repeat
        ACtrl:= FindNextControl(ACtrl,true,true,false);
      until (ACtrl is TCustomEdit) or
      (ACtrl is TCustomComboBox) or
      (ACtrl is TCustomListBox) or
      (ACtrl is TCustomCheckBox) or
      (ACtrl is TRadioButton);

      if ACtrl is TEdit then
        begin
          if assigned(K)  then
            TEdit(ACtrl).OnKeyPress := K;
        end;

      ACtrl.SetFocus ;
    end;

end;
            

Don't forget to set the KeyPreview property of the form to true.

As you can see; my code relies on FindNextControl function which make a nice job to bring the next available control.

since all my data entry forms in many of my applications are inherited from one desendent form so i just put the above code in that single form and voila; all my applications celebrate the event.

 

 

 

 

Shagrouni 2001 Khaled Shagrouni  khaled@shagrouni.com