Recording mouse positions with high sample rate

Hey, I’m working on a project which requires me to record mouse movement with high sample rate(500hz+). And store it in a file.

Any pointers on to how this could be achieved?

You could interpolate the positions between the values you can actually record:

Let’s say your app is running at 100fps - certainly doable if there isn’t much happening graphically or otherwise - record the current mouse position and compare with the last recorded position, then LERP 4 positions in between.

cheers!

This depends on your operating system, but if using an OS that uses GLFW (Windows, OSX, Linux), then there is a way to poll the mouse more often than once per update/draw loop. If using oF 0.9.0, you can call the pollEvents() static function of the ofAppGLFWWindow class. This calls glfwPollEvents(), which is a function of the GLFW windowing system. What glfwPollEvents() does is check for new input events, including mouse events. If there is a new event, the callback function registered with glfwSetCursorPosCallback() is called, giving the current cursor position. You shouldn’t need to use the callback registration function directly, because the ofAppGLFWWindow class registers a listener to the callback and the data from the callback is passed through openFrameworks to your app using the event system.

In short, if you call glfwPollEvents() or ofAppGLFWWindow::pollEvents(), it kicks off a process that results in new mouse cursor information being given to you. If you’re using pre-0.9.0, you can still call glfwPollEvents() directly, but there is no ofAppGLFWWindow::pollEvents() function.

See this page for some information on cursor handling in GLFW: http://www.glfw.org/docs/latest/input.html#cursor_pos I think that you can’t just call glfwGetCursorPos() without first calling glfwPollEvents() to update the cursor position.

Note a caveat: Presumably, if you want 500 Hz sampling, you want fairly even spacing of samples, but this may be difficult to achieve. This is because glfwPollEvents() may only be called from the main thread, but ideally you would like to call it from a secondary thread that sleeps for 2 ms between polling.

2 Likes