Mac OS X: CGGetLastMouseDelta and mouse movement programmatically

I am developing an extension for MATLAB PsychToolbox, which allows you to better control the mouse during psychophysical experiments (in particular, preventing the screen borders from being limited by drag and drop), it should feel like you can move the mouse โ€œendlesslyโ€ in all directions). Since MATLAB does not support the creation of additional threads (and it would be uselessly difficult for this situation anyway), I cannot use the Carbon or Cocoa event managers.

CGGetLastMouseDelta is almost perfect for what I need to do (it receives from me the number of mouse movements โ€œ since the last mouse movement event received by the application โ€, ignoring the borders of the screen), however there is one small problem. When you move the mouse programmatically (using CGWarpMouseCursorPosition or CGDisplayMoveCursorToPoint) events are not generated. Therefore, CGGetLastMouseDelta does not seem to know that the mouse has moved at all. In other words, if I drag 50 pixels and 50 pixels down programmatically, CGGetLastMouseDelta returns (0, 0) subsequently for the delta mouse. This is undesirable behavior in my context and requires ugly workarounds. I tried moving the mouse by passing events through the event system, as follows (this is the โ€œmexFunctionโ€, the MATLAB way to call C code):

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { CGEventRef event; CGPoint offset; CGPoint currentLocation; CGPoint newLocation; if (nrhs != 2) mexErrMsgTxt("The global x and y coordinates (and only those) must be supplied."); event = CGEventCreate(NULL); currentLocation = CGEventGetLocation(event); CFRelease(event); offset = CGPointMake((CGFloat) mxGetScalar(prhs[0]), (CGFloat) mxGetScalar(prhs[1])); newLocation = CGPointMake(currentLocation.x + offset.x, currentLocation.y + offset.y); event = CGEventCreateMouseEvent(NULL, kCGEventMouseMoved, newLocation, kCGMouseButtonLeft); CGEventPost(kCGHIDEventTap, event); CFRelease(event); } 

This happily moves the mouse, but doesn't seem to change the behavior of CGGetLastMouseDelta at all. Does anyone know the exact specifications as to what CGGetLastMouseDelta returns (and when?). Apple's documentation of this material (link to quartz) is as usual nearly useless (or at least the lack of necessary details).

Thanks!

+6
events mouse macos
source share
2 answers

A good idea would be to use CGAssociateMouseAndMouseCursorPosition(0) to disable mouse movement from the cursor. Then you have problems with the borders of the screen.

+2
source share

Option (1) Create your own event that indicates that you made the mouse move.
Option (2) Call the event handler function moved to the mouse using the mouse that moved the mouse.

0
source share

All Articles