Modaldialog doesn't react to enter/esc

delphi, modal-dialog

Solution

From your posted example you can see that the `TSpinEdit` control is focused and captures the keys.

To close the modal form in all cases, set form `KeyPreview` to true and insert this into the `OnKeyPress` event:

procedure TSelectDlg.FormKeyPress(Sender: TObject; var Key: Char);
begin
  if (Key = Char(vk_escape)) then  // #27
    CancelBtn.Click
  else
  if (Key = Char(vk_return)) then  // #13
    OkBtn.Click;    
end;

Problem

I have a modaldialog with an OK and a Cancel button. For the OK I set the `Default` property to True, and for the Cancel button the `Cancel` property. ModalResult is set to `mrOK` and `mrCancel`, resp. However neither pressing the Enter nor the Esc key on my keyboard close the dialog. What did I miss here? edit I posted a small test application using the suspect dialog on my site. IDE is RAD Studio XE3.

Original source