1
$\begingroup$

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?

$\endgroup$
  • $\begingroup$ Please show your code. $\endgroup$ – m_goldberg Jul 17 '14 at 23:54
3
$\begingroup$

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

$\endgroup$
  • $\begingroup$ thanks, I was stucked and this was a part of the problem. $\endgroup$ – jonaprieto Jul 18 '14 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 Jul 18 '14 at 14:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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