Pulkit
Pulkit

Reputation: 4084

Timebox property change for an object and then take action

I have a problem that I am unable to crack and I am looking for pointers from you coding genius :)

Problem

I have an event let's say Event1 which gets fired when the caller takes certain action for the sake of our problem let's say we need to fire Event1 whenever the user presses any key on the keyboard or moves the mouse

class Event1 {
...
    public Event1(String name) {
    }
}


class Runner {

   public static void main(String arg[]){
      while(true) {
          //User enters some text via command line or moves mouse
          Fire(new Event1("key up"));
      }
   }
   private static Fire(Event1 event){
      //Calls some external api
   }
}

If the user is continuously typing, we will end up firing 100s of events per minute. What I want to do is timebox the change i.e. When I detect the user changing the input mechanism from keyboard to mouse or vice versa I fire the event. I am also adding some examples which state when it's ideal to fire the event.

Example

|Event| Keyboard| Mouse   | Keyboard | Mouse   | Keyboard | Keyboard |  Mouse   | Keyboard |
|----| -------- | ------- | -------- | ------- | -------- | -------- | -------- | -------- |
|Time| 12:00:00 |12:00:01 | 12:00:02 |12:00:03 | 12:00:06 | 12:00:07 | 12:00:09 | 12:00:11 | 
|----| -------- | ------- | -------- | ------- | -------- | -------- | -------- | -------- |
|Fire| True     | False   | False   | True    | True     | False    | True     | True     |

I can keep a track of prev event type and decide if the current is same then dont fire the event but I am unable to solve the flickering where the input mechanism keeps on changing frequently.

FYI: I am using Csharp language if that matters :)

Upvotes: 0

Views: 35

Answers (0)

Related Questions