Simple text input field accepting line breaks

2020-06-16 02:36发布

问题:

Is there a simple way to get this to work?

text = "";
DialogInput[{TextCell["Try to type a text with linebreaks :-)"],
  InputField[Dynamic[text], String], 
  Button["Ok", DialogReturn[text]]}]

The problem is that InputField terminates after typing Return. I just want a simple text input field.

回答1:

Thanks for the heads-up Leonid. Here is the code:

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

The FrontEndExecute statement is a little simpler in this version.

Incidentally, to clear the default Return key action of NotebookEventActions you can use NotebookEventActions->{}. This can be useful to stop dialogs disappearing during input.