13

私は Mac アプリケーションを開発していますが、タッチしたときにトラックパッドの指の位置を知りたいです。

それは可能ですか?そうであれば、どのように?

4

4 に答える 4

13

タッチを受け入れるようにビューを設定する必要があります ( [self setAcceptsTouchEvents:YES])。のようなタッチ イベントを取得すると、大きなポイント (1 インチあたり 72 bp あります) に照らして (範囲は [0.0, 1.0] x [0.0, 1.0]) を-touchesBeganWithEvent:見ることで、指がどこにあるかを把握できます。. トラックパッドの左下隅がゼロ原点として扱われます。normalizedPositiondeviceSize

たとえば、次のようになります。

- (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 {
   NSSet *touches = [ev touchesMatchingPhase:NSTouchPhaseBegan 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;
      NSPoint 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);
   }
}

(このコードは実例として扱ってください。実戦で使用されたサンプル コードではありません。コメント ボックスに直接書き込んだだけです。)

于 2010-09-25T01:07:57.723 に答える
1

ObjC インターフェースがあるかどうかはわかりませんが、C HID Class Device Interfaceが興味深いかもしれません。

于 2010-08-26T08:40:37.830 に答える
0

Cocoa(Obj-Cレベル)では、次のことを試してください。ただし、多くのユーザーがまだマウスコントロールを使用していることを忘れないでください。

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

于 2010-08-26T13:26:07.960 に答える