Modaldialog does not respond to / esc input

I have a mod dialog with the OK and Cancel buttons . For OK, I set the Default property to True, and for the Cancel button, the Cancel property. ModalResult is set to mrOK and mrCancel , respectively.

However, neither pressing the Enter key nor the Esc key on my keyboard closes the dialog box. What did I miss here?

change
I posted a small test application using a suspicious dialog on my site. IDE is RAD Studio XE3.

enter image description here

+8
delphi modal-dialog
source share
2 answers

For the record, this should work. However, it looks like TSpinEdit has an error. Since TSpinEdit is a sample (Vcl.Samples.Spin.pas, pay attention to "Samples"), you can fix it yourself.

In TSpinEdit, add the following method declaration only after 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; 

This tells VCL that the edit control does not want to process the Enter and Escape keys (VK_ENTER, VK_ESCAPE). Since it does not process keys, they will be redirected to buttons, which will then be called by the base according to their settings (by default and Cancel).

Feel free to report this to Quality Central

+5
source share

From your published example, you can see that the TSpinEdit control TSpinEdit focused and captures keys.

To close the modal form in all cases, set the KeyPreview form to true and paste it 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; 
+4
source share

All Articles