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.

How can I force the keyboard input to work for this cell without clicking on it after I evaluate or showing a blinking selection cursor? This is for running CDF games in a browser.

pos = {0, 0};
Manipulate[
 EventHandler[
  Framed@Graphics[{Disk[Dynamic[pos], 0.2]}, 
    PlotRange -> 2], {"LeftArrowKeyDown" :> (pos -= {.1, 0}), 
   "RightArrowKeyDown" :> (pos += {.1, 0})}], SaveDefinitions -> True,
  Paneled -> False, AppearanceElements -> None]
share|improve this question
    
I've faced same issue but had no time to focus on this so since I didn't test it in 100% the following is only sugestion: use CellEventActions. Before that I've made a walkarond that on had to click "Start" which automaticaly moved selection on desired object. But at the end you do not want things to be selectable so it is not a good idea. –  Kuba Mar 5 at 7:32
    
NotebookEventActions allows you to set Deployed to True and the events still work. You still have to click once in the window after opening in the browser, but you can click anywhere. –  Michael Hale Mar 5 at 7:45
1  
Yes I know that why I've choosen CellEventActions not EventHandler. –  Kuba Mar 5 at 7:51
    
Ah, if CellEventActions lets you still use Deployed then that is probably better. I was just hoping NotebookEventActions would save you a focus click in the browser. –  Michael Hale Mar 5 at 8:09
1  
Except the code might be slightly simpler for NotebookEventActions because the cell doesn't exist yet when the code is first evaluated. –  Michael Hale Mar 5 at 8:13

1 Answer 1

Using NotebookEventActions seems to work pretty well, but you still have to click once in the window after opening. You can click anywhere though and it allows you to use Deployed to disable all selections and it still works.

So modify the code like below, copy it into new notebook, evaluate, delete input cell, then export entire notebook as CDF.

pos = {0, 0};
SetOptions[InputNotebook[], 
           NotebookEventActions -> {"LeftArrowKeyDown" :> (pos -= {.1, 0}), 
                                    "RightArrowKeyDown" :> (pos += {.1, 0})}];
Manipulate[Framed@Graphics[{Disk[Dynamic[pos], 0.2]}, PlotRange -> 2],
  Deployed -> True, SaveDefinitions -> True, Paneled -> False, 
  AppearanceElements -> None]
share|improve this answer

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.