Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

The return key does not work as expected in an InputField, how can I overcome this?

share|improve this question
Related: stackoverflow.com/questions/6946475/… –  Leonid Shifrin Jun 5 '12 at 18:02
Also related is this MathgGroup thread. –  István Zachar Jun 5 '12 at 18:55
Sure, that might help. I usually search for is:q keyword instead of just the title because most users don't always use a good title. –  rm -rf Feb 24 '14 at 3:13

4 Answers 4

This dialog box version was put together some time ago for Mathematica 7 :-

text = "";
DialogInput[{TextCell["Try to type a text with linebreaks :-)"], 
  InputField[Dynamic[text], String, FieldSize -> {30, 6}], 
 NotebookEventActions -> {"ReturnKeyDown" :> 
    FrontEndExecute[NotebookWrite[InputNotebook[], "\n"]]}]

enter image description here

share|improve this answer

Perhaps something like this?

text = "";
 InputField[Dynamic@text, String, ContinuousAction -> True],
 {"ReturnKeyDown" :> Paste["\n"]}
share|improve this answer
Cool!.. Tks I'll wait to accept just to follow the protocols. :) –  Murta Feb 23 '14 at 16:12

Here is a widget which I constructed some time ago for my purposes. This is an InputField as well, but it operates on boxes, and as a bonus, the standard syntax highlighting works inside it:

Attributes[codeInputField] = {HoldFirst}; 
Options[codeInputField] = {
   BaseStyle -> {FontSize -> 14, FontWeight -> Plain, FontFamily -> "Courier"},
   FieldSize -> {20, {2, Infinity}},
   ImageSize -> Automatic, 
   KeyEventActions -> Automatic,
   EnterPressedCustomFunction :> Automatic

codeInputField[startingCode_, opts : OptionsPattern[]] :=
  With[{bs = OptionValue[BaseStyle], fsize = OptionValue[FieldSize], 
    acts = OptionValue[KeyEventActions],
    EnterPressedF = OptionValue[EnterPressedCustomFunction], 
    ims = OptionValue[ImageSize]},
   Module[{nb, enterPressed},
     With[{actions = 
       Sequence @@ 
        If[acts === Automatic,
           (s_ -> f_) :> (s :> f[Hold[startingCode], Hold[enterPressed], s, nb]),
        enterF  = If[EnterPressedF === Automatic, Hold, EnterPressedF]
             (nb = InputNotebook[]; startingCode = #) &
          FieldSize -> fsize, ContinuousAction -> True, 
          BaseStyle -> bs, ImageSize -> ims
       {"ReturnKeyDown" :>
              enterPressed = True;
              enterF[Hold[startingCode], nb];
              FrontEndExecute[NotebookWrite[InputNotebook[], "\n"]];
              If[! ValueQ[nb], nb = InputNotebook[]];
              SetOptions[NotebookSelection[nb], ShowSelection -> True]; 
       PassEventsDown -> True

Here is an example of use:

code = MakeBoxes[Plot[Sin[x], {x, 0, 4 Pi}]];
     {",RowBox[{"x",",","0",",",RowBox[{"4"," ","\[Pi]"}]}],"}"}]}],"]"}]

The above dynamic is to monitor the code variable. Now, the input field:


enter image description here

The highlighting is enabled after "Enter" is pressed the first time. You can edit the code to see the highlighting at work and how the code variable is updated.

I also coded an interactive REPL-style cell based on this, which can evaluate code inside it. If there is an interest in it, I could expand to include that as well.

share|improve this answer
@Mr.Wizard I just checked, and it works for me, M7.0, Win7 64 bit. What exactly isn't working for you? –  Leonid Shifrin Jun 6 '12 at 8:56
The first time I tried it Mathematica crashed. This time it worked. Still if I press numeric-keypad Return in the input field it crashes. –  Mr.Wizard Jun 6 '12 at 9:24
@Mr.Wizard Interesting. Numeric-keypad Return does not crash it for me, but creates a new cell, instead of just do a carriage return. –  Leonid Shifrin Jun 6 '12 at 9:44
up vote 16 down vote accepted

Ah, figured it out:

Panel @ DynamicModule[{input = ""}, 
  Column[{TextCell["Enter your text here:"], 
     InputField[Dynamic[input], String, ContinuousAction -> True, 
      FieldSize -> {40, 7}], 
     "ReturnKeyDown" :> 
      FrontEndExecute[{NotebookWrite[InputNotebook[], "\n", After]}]
     ], Dynamic@InputForm[input]}]]
share|improve this answer
On a related note: I wonder if that could also be used to intercept "Shift-return" - but I doubt it... –  Jens Jun 5 '12 at 17:53
This doesn't appear to work in version 7. –  Mr.Wizard Jun 6 '12 at 1:10
This solves a long standing question of mine, so thanks! I made an edit for it to handle Shift-Return: EventHandler[ InputField[Dynamic[demo2], String, FieldHint -> "Quarter 1 Turnover", Alignment -> {Left, Center}], {{"MenuCommand", "HandleShiftReturn"} :> FrontEndExecute[{NotebookWrite[InputNotebook[], "\n", After]}], "ReturnKeyDown" :> FrontEndExecute[{NotebookWrite[InputNotebook[], "\n", After]}]}] –  Martin John Hadley Feb 20 '13 at 16:44

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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