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.
Answer
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],
{{"MenuCommand", "HandleShiftReturn"} :> {},
{"MenuCommand", "EvaluateCells"} :> {}} (*edit by Kuba, handles Keypad Enter*)
],
Dynamic[x]
}]
]
Return works, but Shift-Return does not.
Comments
Post a Comment