How to enable Leap Motion to control my cursor in OS X app?

So, I have a game for Mac OS X built into cocos2D.

I use gestures to simulate keyboard commands to control my character, and it works very well.

I sent my game to the AirSpace store and it was rejected on the grounds that the jump should be used to control my menus, which is quite fair, I think.

The thing for life I can’t understand how this is done. There are no examples to show me how to implement it, and nothing in the SDK example, which also makes it clear.

Does anyone have any examples that they would like to split, I only need to grab my cursor and allow the click while holding the button. I really did not think that something so complicated would be required for simple menu navigation.

+4
source share
1 answer

If this is a Mac game, you should have access to the Quartz api event. This is the easiest way to generate mouse events in OS X ...

I would recommend just tracking the position of the palm (hand) and moving the cursor based on this.

Here's how I do palm tracking:

float handX = ([[hand palmPosition] x]); float handY = (-[[hand palmPosition] y] + 150); 

"+ 150" is the number of millimeters above the Leap device that is used as "0". From there, you can move the cursor based on a hand shift from 0.

Function that I use to move the cursor (using quartz):

 - (void)mouseEventWithType:(CGEventType)type loc:(CGPoint)loc deltaX:(float)dX deltaY:(float)dY { CGEventRef theEvent = CGEventCreateMouseEvent(NULL, type, loc, kCGMouseButtonLeft); CGEventSetIntegerValueField(theEvent, kCGMouseEventDeltaX, dX); CGEventSetIntegerValueField(theEvent, kCGMouseEventDeltaY, dY); CGEventPost(kCGHIDEventTap, theEvent); CFRelease(theEvent); } 

and an example of a function call:

 [self mouseEventWithType:kCGEventMouseMoved loc:CGPointMake(newLocX, newLocY) deltaX:dX deltaY:dY]; 

This call will move the mouse. Basically you just pass the new mouse location and the corresponding deltas relative to the last cursor position.

I can provide more examples, such as examples for getting the location of a mouse, a mouse click, or even a complete mouse move program ...

EDIT 1:

To handle a click and drag using quartz, you can call the same function as above, pass only kCGEventLeftMouseDown .

The trap is that you cannot call kCGEventMouseMoved for drag and drop, you must pass kCGEventLeftMouseDragged while dragging.

After completing the drag, you must go through kCGEventLeftMouseUp .

To make one click (without dragging and dropping), you simply call the mouse down, and then right up, without any resistance ...

+4
source

Source: https://habr.com/ru/post/1495439/


All Articles