# Simulate mouse action

**URL:** <https://discourse.processing.org/t/simulate-mouse-action/32312>\
**Category:** Coding Questions\
**Created:** [September 19, 2021, 12:50pm UTC](https://discourse.processing.org/t/simulate-mouse-action/32312 "2021-09-19T12:50:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![potter3366](https://avatars.discourse-cdn.com/v4/letter/p/bb73d2/32.png) [@potter3366](https://discourse.processing.org/u/potter3366)\
**Post date:** [September 19, 2021, 12:50pm UTC](https://discourse.processing.org/t/simulate-mouse-action/32312/1 "2021-09-19T12:50:25Z")

</div>

I have to simulate this mouse action. Instead of a mouse I can use the space button or with the jumps in front of the kinect. How can I control these actions over time or the frequency of repeating something at a certain time ? For example how many “zeros” has arrived the last two seconds or how many false or true have in last two seconds when hit the space bar…

Any ideas?  
Thanks!

```auto
void mouseMoved()
{
  int fx=mouseX-pmouseX, fy=mouseY-pmouseY;
  if (max(abs(fx), abs(fy))>2) {
    DO SOMETHING
  }
}

```

---

<div class="post-metadata">

**Author:** ![rapatski](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rapatski/32/5164_2.png) [@rapatski](https://discourse.processing.org/u/rapatski)\
**Post date:** [September 19, 2021, 8:48pm UTC](https://discourse.processing.org/t/simulate-mouse-action/32312/2 "2021-09-19T20:48:07Z")

</div>

Not entirely sure I understand… are you asking how you can record mouse movements or keypresses and replay them later?

There are different ways you can go about it; storing mouseX and mouseY in an array and writing that to disk so you can load it later? Or you can store keypresses with a hashmap, and document what time they were pressed etc.?

---

<div class="post-metadata">

**Author:** ![potter3366](https://avatars.discourse-cdn.com/v4/letter/p/bb73d2/32.png) [@potter3366](https://discourse.processing.org/u/potter3366)\
**Post date:** [September 19, 2021, 11:55pm UTC](https://discourse.processing.org/t/simulate-mouse-action/32312/3 "2021-09-19T23:55:59Z")

</div>

If, for example, the space key is pressed three times in the last two seconds, then start some action, if it’s not, then nothing. So I have to check the action constantly in real time, for a short past interval. I don’t need a mouse…If I store this values in a array, will it not overflow quickly?

---

<div class="post-metadata">

**Author:** ![rapatski](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rapatski/32/5164_2.png) [@rapatski](https://discourse.processing.org/u/rapatski)\
**Post date:** [September 20, 2021, 12:25pm UTC](https://discourse.processing.org/t/simulate-mouse-action/32312/4 "2021-09-20T12:25:39Z")

</div>

If literally you’d like to define custom ‘key events’, like a triple pressing of the space bar in 2 seconds, or something, I’d indeed make an ArrayList or HashMap that you store the keypresses in, along with a timestamp. You iterate through that array, both counting the keypresses in it, and checking whether their timestamp is older than 2 seconds ago – and chucking them out if so. If it counts three valid keypresses, it fires your action.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [September 20, 2021, 6:01pm UTC](https://discourse.processing.org/t/simulate-mouse-action/32312/5 "2021-09-20T18:01:56Z")

</div>

or when a space key is pressed first time, start timer.

set a boolean timerIsRunning to true

count further space key presses (var countSpace)

after 2 seconds check countSpace and reset timer.
