views:

53

answers:

3

Hello,

I am developping an Mac application and I would like to know the position of the finger in the trackpad when there is a touch.

Is it something possible and if yes, do you have any idea to achieve this?

Thanks in advance for your help,

Regards,

A: 

I don't know if there's an ObjC interface, but you might find the C HID Class Device Interface interesting.

Potatoswatter
It seems to be a really interesting it. I need to look at it deaper. Thanks!
AP
A: 

At a Cocoa (Obj-C level) try the following - although remember that many users are still using mouse control.

http://developer.apple.com/mac/library/documentation/cocoa/conceptual/EventOverview/HandlingTouchEvents/HandlingTouchEvents.html

JulesLt
+1  A: 

Your view needs to be set to accept touches ([self setAcceptsTouchEvents:YES]). When you get a touch event like -touchesBeganWithEvent:, you can figure out where the finger lies by looking at its normalizedPosition (range is [0.0, 1.0] x [0.0, 1.0]) in light of its deviceSize in big points (there are 72 bp per inch). The lower-left corner of the trackpad is treated as the zero origin.

So, for example:

- (id)initWithFrame:(NSRect)frameRect {
   self = [super initWithFrame:frameRect];
   if (!self) return nil;

   /* You need to set this to receive any touch event messages. */
   [self setAcceptsTouchEvents:YES]

   /* You only need to set this if you actually want resting touches.
    * If you don't, a touch will "end" when it starts resting and
    * "begin" again if it starts moving again. */
   [self setWantsRestingTouches:YES]
   return self;
}

/* One of many touch event handling methods. */
- (void)touchesBeganWithEvent:(NSEvent *)ev {
   NSArray *touches = [ev touchesMatchingPhase:NSTouchPhasesBegan inView:self];
   for (NSTouch *touch in touches) {
      /* Once you have a touch, getting the position is dead simple. */
      NSPoint fraction = touch.normalizedPosition;
      NSSize whole = touch.deviceSize;
      NSSize wholeInches = {whole.width / 72.0, whole.height / 72.0};
      NSPoint pos = wholeInches;
      pos.x *= fraction.x;
      pos.y *= fraction.y;
      NSLog(@"%s: Finger is touching %g inches right and %g inches up "
            @"from lower left corner of trackpad.", __func__, pos.x, pos.y);
   }
}

(Treat this code as an illustration, not as tried and true, battle-worn sample code; I just wrote it directly into the comment box.)

Jeremy W. Sherman