7
$\begingroup$

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]
$\endgroup$
5
  • $\begingroup$ 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. $\endgroup$
    – Kuba
    Commented Mar 5, 2014 at 7:32
  • $\begingroup$ 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. $\endgroup$ Commented Mar 5, 2014 at 7:45
  • 1
    $\begingroup$ Yes I know that why I've choosen CellEventActions not EventHandler. $\endgroup$
    – Kuba
    Commented Mar 5, 2014 at 7:51
  • $\begingroup$ 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. $\endgroup$ Commented Mar 5, 2014 at 8:09
  • 1
    $\begingroup$ Except the code might be slightly simpler for NotebookEventActions because the cell doesn't exist yet when the code is first evaluated. $\endgroup$ Commented Mar 5, 2014 at 8:13

1 Answer 1

3
$\begingroup$

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]
$\endgroup$

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.