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.

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[]]}]
share|improve this question
    
I look forward to seeing the answer to this one. –  Mr.Wizard Feb 7 '12 at 22:42

2 Answers 2

up vote 12 down vote accepted

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]
share|improve this answer
    
+1 for low level digging –  Mr.Wizard Feb 8 '12 at 0:01
    
+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. –  Leonid Shifrin Feb 8 '12 at 9:22

What's wrong with Input[""]?

share|improve this answer
    
I must assume it is not general enough, that he is building an entire dialog and that example given is quite reduced. –  Mr.Wizard Feb 7 '12 at 22:51
    
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! –  István Zachar Feb 7 '12 at 23:29
    
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? –  David Carraher Feb 8 '12 at 0:04
3  
@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. –  István Zachar Feb 8 '12 at 0:13
    
Thanks, István. –  David Carraher Feb 8 '12 at 0:25

Your Answer

 
discard

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.