Modaldialog doesn't react to enter/esc

白昼怎懂夜的黑 提交于 2019-12-05 03:22:42

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;

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!