3
$\begingroup$

When InputField was set to String, I found a way to change the line of the sentence by pressing Enter. In this case, how does the calculation run when the Shift+Enter key is pressed?

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

enter image description here

I want to write a few lines of instruction in the InputField and run it when I press Shift+Enter.

$\endgroup$

1 Answer 1

4
$\begingroup$

Combining your attempt with the technique from this answer:

multilineInput[Dynamic[text_]] := DynamicModule[
  {innerText = If[StringQ@text, text, ""]},
  EventHandler[
   InputField[Dynamic@innerText, String],
   {
    "ReturnKeyDown" :> If[! CurrentValue["ShiftKey"], Paste["\n"]],
    {"MenuCommand", 
      "HandleShiftReturn"} :> {text = innerText}, {"MenuCommand", 
      "EvaluateCells"} :> {text = innerText}
    },
   PassEventsDown -> False
   ]
  ]

text = "Hello world";
multilineInput[Dynamic@text]
Dynamic@text

enter image description here

$\endgroup$
1
  • $\begingroup$ It's great! It was a great help! Thank you~^^ $\endgroup$
    – Milk
    Commented Apr 17, 2023 at 13:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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