# Sampling mouse input at predefined frequency

**URL:** <https://mworks.discourse.group/t/sampling-mouse-input-at-predefined-frequency/464>\
**Category:** Support\
**Created:** [February 10, 2016, 2:52pm UTC](https://mworks.discourse.group/t/sampling-mouse-input-at-predefined-frequency/464 "2016-02-10T14:52:33Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [February 10, 2016, 2:52pm UTC](https://mworks.discourse.group/t/sampling-mouse-input-at-predefined-frequency/464/1 "2016-02-10T14:52:33Z")

</div>

Hi Chris,

I was wondering if it is possible to sample the mouse\_x and mouse\_y at a predefined frequency in mworks,(1kHz similar to eye\_x and eye\_y).

Thanks,  
Eghbal

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [February 10, 2016, 3:24pm UTC](https://mworks.discourse.group/t/sampling-mouse-input-at-predefined-frequency/464/2 "2016-02-10T15:24:34Z")

</div>

Hi Eghbal,

> I was wondering if it is possible to sample the mouse\_x and mouse\_y at a predefined frequency in mworks,(1kHz similar to eye\_x and eye\_y).

You can achieve this using scheduled actions. See the attached example experiment for a demonstration.

A couple important points:

- A `repeats` value of -999 means “repeat forever”.
- The variable associated with the `cancel` parameter must be set to 1 before the experiment stops. Otherwise, the scheduled actions will continue indefinitely, even after the experiment is closed. (This is really a bug that needs to be fixed).

Cheers,  
Chris

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [February 10, 2016, 3:24pm UTC](https://mworks.discourse.group/t/sampling-mouse-input-at-predefined-frequency/464/3 "2016-02-10T15:24:34Z")

</div>

Attachment: [mouse\_input\_1kHz.xml](https://mworks.discourse.group/uploads/short-url/zuOcILb5yKJrlMTmkak5GIe9vDY.xml) (3.11 KB)

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [February 10, 2016, 8:32pm UTC](https://mworks.discourse.group/t/sampling-mouse-input-at-predefined-frequency/464/4 "2016-02-10T20:32:53Z")

</div>

Hi Chris,  
Thanks for the code ,  
I am looking at the time steps of mouse updates, and it is variable through the ranging from ~950 us to ~1050 us.Is this expected?  
Thanks  
Eghbal

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [February 11, 2016, 2:50pm UTC](https://mworks.discourse.group/t/sampling-mouse-input-at-predefined-frequency/464/5 "2016-02-11T14:50:39Z")

</div>

Hi Eghbal,

> I am looking at the time steps of mouse updates, and it is variable through the ranging from ~950 us to ~1050 us.Is this expected?

Yes, that seems reasonable. The waits between repetitions are software timed, so there’s going to be some variability in their durations. (This is opposed to, say, NIDAQ analog input, which is hardware timed, with each sample triggered by an onboard clock.)

Cheers,  
Chris

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [February 11, 2016, 3:20pm UTC](https://mworks.discourse.group/t/sampling-mouse-input-at-predefined-frequency/464/6 "2016-02-11T15:20:11Z")

</div>

Hi Chris,  
Thanks,  
Is it possible to reduce the variability if I go to lower sampling rate (~200Hz). Ideally, I want to have a fixed sampling interval for the variable.

Eghbal

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [February 11, 2016, 8:14pm UTC](https://mworks.discourse.group/t/sampling-mouse-input-at-predefined-frequency/464/7 "2016-02-11T20:14:53Z")

</div>

> Is it possible to reduce the variability if I go to lower sampling rate (~200Hz). Ideally, I want to have a fixed sampling interval for the variable.

No, that won’t make any difference. With software-timed waits on a non-realtime operating system like OS X, getting to within a few hundred microseconds of your target is the best you’re likely to do.

Chris

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [February 19, 2016, 5:55pm UTC](https://mworks.discourse.group/t/sampling-mouse-input-at-predefined-frequency/464/8 "2016-02-19T17:55:34Z")

</div>

Hi Chris,

Sorry I had a quick question as I am trying to use the predefined sampling to update the cursor shown on the screen.  
I noticed that if I add a frame list for the cursor display, the update in mouse position happens at a slower sample rate ~150Hz even though the update on mouse is at 1kHz.

Thanks,  
Eghbal

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [February 19, 2016, 5:55pm UTC](https://mworks.discourse.group/t/sampling-mouse-input-at-predefined-frequency/464/9 "2016-02-19T17:55:34Z")

</div>

Attachment: [mouse\_input\_1kHz\_eh.xml](https://mworks.discourse.group/uploads/short-url/sDOkI1rrawse9heg6MzpzJ3RDwz.xml) (3.75 KB)

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [February 20, 2016, 1:31pm UTC](https://mworks.discourse.group/t/sampling-mouse-input-at-predefined-frequency/464/10 "2016-02-20T13:31:39Z")

</div>

Hi Eghbal,

By “update in mouse position”, do you mean changes to the mouse\_x/mouse\_y values, or changes in the position of the cursor on screen? I see that your cursor\_dot stimulus is using raw\_mouse\_x/raw\_mouse\_y for its position, so its position will change only when the mouse actually moves.

Also, if the Schedule Actions that sets mouse\_x/mouse\_y falls behind schedule by more than 5ms, you should see warnings and/or errors in the server console. Have you noticed these? I don’t see any when I run your example.

Thanks,  
Chris

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [February 22, 2016, 8:02pm UTC](https://mworks.discourse.group/t/sampling-mouse-input-at-predefined-frequency/464/11 "2016-02-22T20:02:08Z")

</div>

Hey Chris,  
Sorry about the confusion, I wanted to get a readout of mouse\_x/mouse\_y values, cursor updates correctly on the screen.  
I don’t see the error too, but when I look at the data, there are multiple rows(around 7 samples) with the same position values, so the effective sampling frequency is about 150Hz. I have attached an example.

Thanks,  
Eghbal

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [February 22, 2016, 8:02pm UTC](https://mworks.discourse.group/t/sampling-mouse-input-at-predefined-frequency/464/12 "2016-02-22T20:02:08Z")

</div>

Attachments:

- [test.jpg](https://mworks.discourse.group/uploads/short-url/dIdhHGQM7iHut50DUQks1buiZps.jpeg) (9.76 KB)
- [test.fig](https://mworks.discourse.group/uploads/short-url/yyURoqQURSQKtOYqWczDYEZXmEl.fig) (29.7 KB)

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [March 2, 2016, 2:54pm UTC](https://mworks.discourse.group/t/sampling-mouse-input-at-predefined-frequency/464/13 "2016-03-02T14:54:35Z")

</div>

To close this out: Eghbal was hoping to increase the rate at which the mouse position was actually updated (with new position data) to 1kHz. However, MWorks’ mouse interface relies on mouse movement events delivered by the OS, and it can’t control the rate at which those events are sent. As an alternative, we might try getting the mouse input data at a lower level (e.g. the raw USB HID events), but that would be complicated and might not yield a better data resolution. I suggested that Eghbal consider using alternative, possibly custom-designed hardware for user input.

---

<div class="post-metadata">

**Author:** ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)\
**Post date:** [July 19, 2022, 10:09pm UTC](https://mworks.discourse.group/t/sampling-mouse-input-at-predefined-frequency/464/14 "2022-07-19T22:09:55Z")

</div>


