How can I make an InputField with a proper newline/carriage return?

Ah, figured it out:

Panel @ DynamicModule[{input = ""}, 
  Column[{TextCell["Enter your text here:"], 
    EventHandler[
     InputField[Dynamic[input], String, ContinuousAction -> True, 
      FieldSize -> {40, 7}], 
     "ReturnKeyDown" :> 
      FrontEndExecute[{NotebookWrite[InputNotebook[], "\n", After]}]
     ], Dynamic@InputForm[input]}]]

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:

ClearAll[codeInputField];
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,
          {},
          Replace[acts, 
           (s_ -> f_) :> (s :> f[Hold[startingCode], Hold[enterPressed], s, nb]),
           {1}]
        ],
        enterF  = If[EnterPressedF === Automatic, Hold, EnterPressedF]
      },
     EventHandler[
       InputField[
          Dynamic[
             startingCode, 
             (nb = InputNotebook[]; startingCode = #) &
          ],
          Boxes, 
          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]; 
          ),
          actions
       },
       PassEventsDown -> True
  ]]]];

Here is an example of use:

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

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

codeInputField[code]

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.


Perhaps something like this?

text = "";
EventHandler[
 InputField[Dynamic@text, String, ContinuousAction -> True],
 {"ReturnKeyDown" :> Paste["\n"]}
 ]