1
$\begingroup$

So here is some code that successfully displays a slider, the value of a dynamic variable, and cycles through a list of 10 images titled 'imgs' with keyboard commands.

ExpressionCell[
 Column@{Slider[Dynamic[y], {1, 10, 1}], Dynamic@y, 
   Dynamic[imgs[[y]]]}, 
   CellEventActions -> {"LeftArrowKeyDown" :> (y = 
    Evaluate[Max[1, y - 1]]), 
    "RightArrowKeyDown" :> (y = Evaluate[Min[10, y = y + 1]])}]

However, when I put this code into DynamicModule[{y},"Insert previous block of code here"]

The keyboard functionality ceases and when trying to use the keyboard I get there error: "Recursion depth of 1024 exceeded during evaluation of -1+y$$."

How can I get the CellEventActions to work properly within DynamicModule?

Much thanks in advance!

$\endgroup$

1 Answer 1

2
$\begingroup$

This should work as you expect:

DynamicModule[{y}, 
 CellPrint[
  TextCell[Column@{Slider[Dynamic[y], {1, 10, 1}], Dynamic@y, Dynamic[imgs[[y]]]}, 
   CellEventActions -> {"LeftArrowKeyDown" :> (y = Max[1, y - 1]; 
       SelectionMove[InputNotebook[], After, ButtonCell]), 
     "RightArrowKeyDown" :> (y = Min[10, y = y + 1]; 
       SelectionMove[InputNotebook[], Before, ButtonCell]), 
     PassEventsDown -> True}, ShowSelection -> False]]]

The first change I have done is to use CellPrint instead of ExpressionCell and thus avoiding the recursion depth error. Then I used SelectionMove so to keep the selection inside the cell, after the operation on y is done. I hope this is what you are looking for.

$\endgroup$
1
  • $\begingroup$ Thank you! Works great. SelectionMove is something new for me, thanks for sharing. $\endgroup$
    – DarrenM
    Jun 22, 2015 at 17:56

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.