In InputField, how to prevent Shift-Return from generating a new cell?

When running the following in a notebook (not a deployed CDF file), it can sometimes be annoying that accidentally pressing Shift-Return breaks out of the user interface:

Panel@DynamicModule[{input = ""}, InputField[Dynamic[input], String]]


I would like to know if one can (programmatically) prevent such a cell from being "evaluated" when Shift-Return is pressed, so that the user stays in the InputField instead of being thrown out of it while seeing a new generated cell appear.

• One quick hack is to define a custom evaluator for the cell in which the InputField is, and make it return Null at the end. You can test by setting it to a simple CellEvaluationFunction->(Null& ). Jun 5 '12 at 19:19
• @LeonidShifrin I tried that, and it's very close because it doesn't generate a new cell. But it still evaluates silently, increases the history counter and puts the cursor in the next line.
– Jens
Jun 5 '12 at 19:24
• No time to do this myself, but one idea is examining the help browser's docked cell which also has an InputField in it. Jun 5 '12 at 19:34
• @Szabolcs That sounds like a good idea. Get help from the help browser in an unconventional way...
– Jens
Jun 5 '12 at 19:37
• @Jens I briefly looked at it and it seems to be defined as a "front end resource" (not sure what that is). The next step would be searching the installation files for this resource (e.g. even the Find window is a notebook that you can examine---and yes, that has an input field too, maybe we don't need to go to the help browser!) ... Okay, I got to sleep now, I need to catch the Venus transit tomorrow at dawn :-) Jun 5 '12 at 19:55

If you notice Mathematica Documentation Center search field is kind of the same thing. And search window from Ctrl-F too. It uses a similar concept to what I'll show. Important thing to know is that EventHandler can track commands from the Mathematica menu. There is an undocumented option HandleShiftReturn and here is a trick I learned from FW:

DynamicModule[{x = ""},
Column[{
EventHandler[
InputField[Dynamic[x], String],
],
Dynamic[x]
}]
]


Return works, but Shift-Return does not.

• Ah, finally: I had been trying to guess what the Menu command for "Evaluate Cells" might be and gave up guessing a while ago...
– Jens
Jun 5 '12 at 20:47
• users could be saved a lot of time if all this stuff was documented. Jun 5 '12 at 23:56
• @Vitaliy could you please add whatever you know about events for EventHandler as an answer to this question: mathematica.stackexchange.com/questions/6207/…. thanks Jun 6 '12 at 0:07
• This also catches Shift+Return but not keypad Return. Is there a way to extend it? Jun 6 '12 at 1:18
• @Mr.Wizard add {"MenuCommand", "EvaluateCells"} event handler, 47513
– Kuba
May 22 '15 at 6:47
ExpressionCell[Panel@DynamicModule[{input = ""}, InputField[Dynamic[input], String]]]


ShiftReturn pressed:

Learned this here.

• It works! I also combined it with Mike's answer to a related issue and it still worked in that more complicated setting, too.
– Jens
Jun 5 '12 at 20:35
• @jens Glad to hear. I still find ExpressionCell a somewhat mysterious function. Jun 5 '12 at 20:44
• This catches Shift+Return but not keypad Return. Any comment on that? Jun 6 '12 at 1:17
• @Mr.Wizard Sorry, I'm on a laptop without keypad. Can't test. Jun 6 '12 at 5:28

This is just adding a Deployed->True to your output cell, and removing the CellEditDuplicate which ruins everything

CellPrint@
ExpressionCell[
Panel@DynamicModule[{input = ""},
InputField[Dynamic[input], String]], "Output",
CellEditDuplicate -> False, Deployed -> True];


Actually, with CellEditDuplicate->False should be enough. However, keep Deployed in mind if you use other styles, and so Evaluatable->False

There appears to be an undocumented InputBoxOptions "TrapEnterKey" which does just that:

InputField[
Dynamic[x]
, String
, BaseStyle -> InputFieldBoxOptions -> {"TrapEnterKey" -> True}
]