Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modaldialog doesn't react to enter/esc

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.

enter image description here

like image 263
stevenvh Avatar asked Jul 05 '14 07:07

stevenvh


2 Answers

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;
like image 167
LU RD Avatar answered Oct 17 '22 08:10

LU RD


For the record, this should work. However, it seems that TSpinEdit has a bug. Since TSpinEdit is a sample (Vcl.Samples.Spin.pas, note the "Samples"), you can fix this yourself.

To TSpinEdit, add the following method declaration just following WMCut:

   procedure WMGetDlgCode(var Message: TWMGetDlgCode); message WM_GETDLGCODE;

Complete the class (Shift+Ctrl+C) and add the following code to WMGetDlgCode:

procedure TSpinEdit.WMGetDlgCode(var Message: TWMGetDlgCode);
begin
  inherited;
  Message.Result := Message.Result and not DLGC_WANTALLKEYS;
end;

That will tell VCL that the edit control doesn't want to process the Enter and Escape keys (VK_ENTER, VK_ESCAPE). Since it doesn't process the keys, they'll be forwarded to the buttons, which will then be invoked base on their settings (Default & Cancel).

Feel free to report this at Quality Central

like image 27
Allen Bauer Avatar answered Oct 17 '22 08:10

Allen Bauer