As you can see in the documentation of CreateDialog

In dialogs created with CreateDialog, pressing Return is effectively equivalent to clicking the DefaultButton, and Esc to clicking the CancelButton.

If the dialog contains no DefaultButton or CancelButton, pressing Return or Esc will close the dialog without taking any other action.

I have a input field, for example, and I often press Enter key after I wrote my inputs. That what happened is that my window close immediately after that.

How can change this behavior in Dialog?

  • $\begingroup$ Please show your code. $\endgroup$
    – m_goldberg
    Commented Jul 17, 2014 at 23:54

2 Answers 2


Note the second paragraph you've cited:

If the dialog contains no DefaultButton or CancelButton, pressing Enter or Esc will close the dialog without taking any other action.

It turns out the inverse is true as well; you can make the dialog notebook contain an invisible DefaultButton that doesn't do anything. Here's an example with Overlay:

CreateDialog[Overlay[{DefaultButton[], InputField[]}, {2}, 2]]

In the arguments to Overlay, {2} says to only display the second element (the InputField), and 2 says to allow selections etc. in the second element (again, the InputField).

Note there's no need to specify DefaultButton beyond its defaults. Also, I'm a little surprised that Overlay can make the DefaultButton "present" like this but at the same time "turned off". But there it seems to be. Good for this situation at least :)

  • $\begingroup$ thanks, I was stucked and this was a part of the problem. $\endgroup$ Commented Jul 18, 2014 at 3:36
  • 1
    $\begingroup$ @d555, no problem, glad to help. I updated the answer a bit, turns out the second argument of Overlay can be used to make the DefaultButton "present" yet invisible still. So it's simpler than I'd originally explained. $\endgroup$
    – William
    Commented Jul 18, 2014 at 14:15

In a dialog window, use the option NotebookEventActions -> {"WindowClose" :> {}} to avoid any default behaviors associated with the ESC or Enter keys. In the example below, even the DefaultButton won't give DialogReturn[] if you press Enter.

DefaultButton["close", DialogReturn[]],
NotebookEventActions -> {"WindowClose" :> {}}]

However, this dialog can be closed via keyboard shortcuts such as CMD + W on Mac, and Alt + F4 on Windows. If you want to remove that functionality as well, add WindowFrameElements -> {}, which removes the upper close button, which then removes the keyboard shortcuts for those buttons. In this case, you must use the DialogReturn[] button to close the Dialog. See below.

 DefaultButton["close", DialogReturn[]], 
 NotebookEventActions -> {"WindowClose" :> {}},
 WindowFrameElements -> {}]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.