22
$\begingroup$

I have a dialog, which, when pops up, requests input from the user. How can I set the window up that the focus is on the InputField of the new window (i.e. the caret stands in the InputField), so that when the user starts typing, it is immediately registered by the field? At the moment, I have to click inside the field first to make it the active control on screen.

DialogInput[{InputField["", String], Button["Ok", DialogReturn[]]}]
$\endgroup$
1
  • $\begingroup$ I look forward to seeing the answer to this one. $\endgroup$
    – Mr.Wizard
    Feb 7, 2012 at 22:42

3 Answers 3

12
$\begingroup$

After István Zachar's points, I was investigating Input definitions to learn more. It seams that 2 years later WRI changed approach from SelectionMove based to more automatic BoxReferenceFind.

usage

So what we only have to do is to set BoxID option for fields of interest and find those references when we want, with:

MathLink`CallFrontEnd[
 FrontEnd`BoxReferenceFind[ 
   FE`BoxReference[
     _NotebookObject, {{ID_String}}, 
     FE`BoxOffset -> {FE`BoxChild[1]},   
     FE`SearchStart -> "StartFromBeginning"
   ]
 ]
]

This is a way more flexible approach, e.g. you can easily put InputField somewhere else and you don't have to change SelectionMove steps to get there.

example

DynamicModule[{name = "", surname = "", setFocus}
  , Column[{
        InputField[Dynamic@name, String, BoxID -> "name"]
      , InputField[Dynamic@surname, String, BoxID -> "surname"]
      , Button["setFocusToFirst", setFocus[EvaluationNotebook[], "name"]]
    }]
  , SynchronousInitialization -> False
  , Initialization :> (
        setFocus[nb_, ID_] :=  MathLink`CallFrontEnd[
            FrontEnd`BoxReferenceFind[ FE`BoxReference[
                nb
              , {{ID}}
              , FE`BoxOffset -> {FE`BoxChild[1]}
              , FE`SearchStart -> "StartFromBeginning"
            ]]
        ]
      ; setFocus[EvaluationNotebook[], "surname"]
    )
]
$\endgroup$
12
  • 7
    $\begingroup$ It is great that this can be done. It is a travesty that users have to poke around in the internals of Mathematica to accomplish a simple task like this. $\endgroup$
    – John McGee
    May 1, 2015 at 10:21
  • $\begingroup$ Now it's only missing the tab key jump order control ! $\endgroup$
    – P. Fonseca
    May 4, 2015 at 11:48
  • $\begingroup$ @P.Fonseca On Windows it switches InputFields with Tab quite well. $\endgroup$
    – Kuba
    May 4, 2015 at 11:55
  • $\begingroup$ Right. But through which order? Can you change the default order? In most visual languages, you can easily change the order of these jumps... $\endgroup$
    – P. Fonseca
    May 4, 2015 at 12:33
  • 1
    $\begingroup$ @jjc385 It really breaks, it seems that CellDynamicExpression wasn't fully fixed: mathematica.stackexchange.com/q/126655/5478. I will delete this example as this is a long term problem and I don't believe it will be fixed anytime soon. Thanks for a feedback. $\endgroup$
    – Kuba
    May 29, 2017 at 11:13
14
$\begingroup$

Since Input is a DialogInput, it seemed reasonable to peek into Input's structure to understand how the focus is set. After removing the ReadProtected attribute I've realized that there is no neat way to do it, as WRI itself has done the reposition of the focus via successive SelectionMove calls.

This example below is not the original but a modified version, as the original Input definition only includes one InputField but no other expressions (like a Button). Note that it is not documented that you can use Initialization in dialogs, moreover it is colored red by the syntax highlighter to suggest an invalid option.

DialogInput[{InputField["", String], Button["Ok", DialogReturn[]]},
 Initialization :> (FrontEndExecute[{
      FrontEnd`SelectionMove[#1, Before, Notebook, 
       AutoScroll -> False],
      FrontEnd`SelectionMove[#1, Next, Cell, 2, AutoScroll -> False],
      FrontEnd`SelectionMove[#1, Previous, CellContents, 
       AutoScroll -> False],
      FrontEnd`FrontEndToken[#1, "MovePreviousPlaceHolder"]
      }] &), ShowCellBracket -> True, Selectable -> True]
$\endgroup$
4
  • $\begingroup$ +1. I used similar code for my syntax highlighter generator, but wasn't succesful in trying to adopt it here. Good that you managed - another good example of how this is done. $\endgroup$ Feb 8, 2012 at 9:22
  • 2
    $\begingroup$ @Mr.Wizard Leonid and Istvan, you all may be interested in fact that WRI changed approach and use now FrontEnd`ReferenceBoxFind which is quite handy and more general. $\endgroup$
    – Kuba
    Apr 2, 2015 at 7:38
  • $\begingroup$ @Kuba This seems interesting, but do you possess any further information about where, how, and who uses ReferenceBox* and BoxReference* functions? It is hard to experiment with them without any pointer on what they do. $\endgroup$ Apr 10, 2015 at 16:23
  • $\begingroup$ Only from my own tests, and not much. E.g you can put BoxID almost everythere and it can be simple string. $\endgroup$
    – Kuba
    Apr 10, 2015 at 18:36
4
$\begingroup$

What's wrong with Input[""]?

$\endgroup$
4
  • $\begingroup$ I must assume it is not general enough, that he is building an entire dialog and that example given is quite reduced. $\endgroup$
    – Mr.Wizard
    Feb 7, 2012 at 22:51
  • $\begingroup$ Nothing is wrong with Input but as Spartacus has pointed out, it is not general enough. Nevertheless, this pointed me to the right direction, so thanks David! $\endgroup$ Feb 7, 2012 at 23:29
  • $\begingroup$ Spartacus is right. My suggestion was really aimed at quick fix. By the way, István, how did you manage to peek into Input's structure? $\endgroup$
    – DavidC
    Feb 8, 2012 at 0:04
  • 3
    $\begingroup$ @David: The usual way: by removing all attributes (Attributes[Input] = {}) and then querying the definition as ??Input. This trick could give useful insight only if the symbol is ReadProtected (meaning that most of its definition is written in Mathematica instead of C or whatnot), or not even then. $\endgroup$ Feb 8, 2012 at 0:13

Your Answer

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

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