# How to refuse close de Dialog when press Enter Key

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?

• Please show your code. – m_goldberg Jul 17 '14 at 23:54

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 :)

• thanks, I was stucked and this was a part of the problem. – jonaprieto Jul 18 '14 at 3:36
• @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. – William Jul 18 '14 at 14:15