16
$\begingroup$

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.

$\endgroup$
7
  • 1
    $\begingroup$ 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& ). $\endgroup$ Jun 5, 2012 at 19:19
  • $\begingroup$ @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. $\endgroup$
    – Jens
    Jun 5, 2012 at 19:24
  • 1
    $\begingroup$ No time to do this myself, but one idea is examining the help browser's docked cell which also has an InputField in it. $\endgroup$
    – Szabolcs
    Jun 5, 2012 at 19:34
  • 2
    $\begingroup$ @Szabolcs That sounds like a good idea. Get help from the help browser in an unconventional way... $\endgroup$
    – Jens
    Jun 5, 2012 at 19:37
  • $\begingroup$ @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 :-) $\endgroup$
    – Szabolcs
    Jun 5, 2012 at 19:55

4 Answers 4

15
$\begingroup$

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]
   }]
]

enter image description here

Return works, but Shift-Return does not.

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

Mathematica graphics
Content added:

Mathematica graphics

ShiftReturn pressed:

Mathematica graphics

Learned this here.

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

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

$\endgroup$
1
$\begingroup$

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

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

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.