私は Mac アプリケーションを開発していますが、タッチしたときにトラックパッドの指の位置を知りたいです。
それは可能ですか?そうであれば、どのように?
私は Mac アプリケーションを開発していますが、タッチしたときにトラックパッドの指の位置を知りたいです。
それは可能ですか?そうであれば、どのように?
タッチを受け入れるようにビューを設定する必要があります ( [self setAcceptsTouchEvents:YES]
)。のようなタッチ イベントを取得すると、大きなポイント (1 インチあたり 72 bp あります) に照らして (範囲は [0.0, 1.0] x [0.0, 1.0]) を-touchesBeganWithEvent:
見ることで、指がどこにあるかを把握できます。. トラックパッドの左下隅がゼロ原点として扱われます。normalizedPosition
deviceSize
たとえば、次のようになります。
- (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);
}
}
(このコードは実例として扱ってください。実戦で使用されたサンプル コードではありません。コメント ボックスに直接書き込んだだけです。)
ObjC インターフェースがあるかどうかはわかりませんが、C HID Class Device Interfaceが興味深いかもしれません。
Cocoa(Obj-Cレベル)では、次のことを試してください。ただし、多くのユーザーがまだマウスコントロールを使用していることを忘れないでください。