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.

What might be the most elegant way to throttle the a,s functions triggered by an EventHandler such that the InputField doesn't get to far behind in the following example? Just to clarify. I am looking to have the queued functions stop executing after either the a or s keys are released.

Here is an illustration of a similar issue to the one I am experiencing while in the Graphics3D element.

i = 0;
a[] := (Pause[RandomInteger[{1, 9}]*.1]; i++;);
s[] := (Pause[RandomInteger[{1, 9}]*.1]; i--;);
EventHandler[InputField[Dynamic@i, Number], {"KeyDown" :> (
    If[CurrentValue["EventKey"] == "a",
     a[], s[]
     ]
    )}
 ]

EDIT: If you hold down the a key I would like the unrun functions to either not run or somehow throttle the number of times the function is executed such that you can successfully hold down the a key for 2 seconds and then immediately(or as quickly as possible) hold down s key to subtract without a delay in the response. Ideally I would like to use the EventHandler KeyReleased option but I am currently unable to get such key to work.

share|improve this question
    
Just to clarify, does "too far behind" mean your example should not increment/decrement when I press a key multiple times during the pause or are you hoping for some type of buffered response? –  bobthechemist Sep 21 at 14:46
    
@bobthechemist If you hold down the a key I would like the unrun functions to either not run or somehow throttle the number of times the function is executed such that you can successfully hold down the a key for 2 seconds and then immediately(or as quickly as possible) hold down s key to subtract without a delay in the response. –  Liam William Sep 21 at 16:58
    
@bobthechemist I believe KeyReleased would make this process a lot easier although I have been unable to get such EventHandler to work currently –  Liam William Sep 21 at 17:08

1 Answer 1

This doesn't work completely for reasons I don't know why, but the following seems to help some by using an InputField to monitor the keyboard input.

str = "";
i = 0;
a[] := (Pause[RandomInteger[{1, 9}]*.1]; i++;);
s[] := (Pause[RandomInteger[{1, 9}]*.1]; i--;);
Dynamic[Refresh[
  t = 0;
  Map[
   (If[# == "a", a[], s[]]) &,
   Select[StringSplit[str, ""], t++ < 3 &]
   ];
  str = "";
  , UpdateInterval -> 1], TrackedSymbols -> {}]
InputField[Dynamic@str, String, ContinuousAction -> True]
InputField[Dynamic@i, Number]
share|improve this answer
    
The following is relevant pastebin.com/raw.php?i=uzF8sGTm –  Liam William Oct 14 at 11:20

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.