知道 Mac OS X 下手指在触控板上的位置

发布于 2024-09-16 05:42:59 字数 65 浏览 1 评论 0原文

我正在开发一个 Mac 应用程序,我想知道触摸时手指在触控板上的位置。

有可能吗?如果有,怎么可能?

I am developing 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, how?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

攒眉千度 2024-09-23 05:42:59

您的视图需要设置为接受触摸([self setAcceptsTouchEvents:YES])。当您收到像 -touchesBeganWithEvent: 这样的触摸事件时,您可以通过查看其 normalizedPosition 来确定手指所在的位置(范围为 [0.0, 1.0] x [0.0, 1.0]),因为它的 deviceSize 是大点(每英寸有 72 bp)。触控板的左下角被视为零原点。

因此,例如:(

- (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);
   }
}

将此代码视为示例,而不是经过验证的真实示例代码;我只是将其直接写到注释框中。)

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 {
   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);
   }
}

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

嗼ふ静 2024-09-23 05:42:59

Swift 3:

我编写了一个 NSTouch 扩展,它返回相对于 NSView 的触控板触摸位置:

extension NSTouch {
    /**
     * Returns the relative position of the touch to the view
     * NOTE: the normalizedTouch is the relative location on the trackpad. values range from 0-1. And are y-flipped
     * TODO: debug if the touch area is working with a rect with a green stroke
     */
    func pos(_ view:NSView) -> CGPoint{
        let w = view.frame.size.width
        let h = view.frame.size.height
        let touchPos:CGPoint = CGPoint(self.normalizedPosition.x,1 + (self.normalizedPosition.y * -1))/*flip the touch coordinates*/
        let deviceSize:CGSize = self.deviceSize
        let deviceRatio:CGFloat = deviceSize.width/deviceSize.height/*find the ratio of the device*/
        let viewRatio:CGFloat = w/h
        var touchArea:CGSize = CGSize(w,h)
        /*Uniform-shrink the device to the view frame*/
        if(deviceRatio > viewRatio){/*device is wider than view*/
            touchArea.height = h/viewRatio
            touchArea.width = w
        }else if(deviceRatio < viewRatio){/*view is wider than device*/
            touchArea.height = h
            touchArea.width = w/deviceRatio
        }/*else ratios are the same*/
        let touchAreaPos:CGPoint = CGPoint((w - touchArea.width)/2,(h - touchArea.height)/2)/*we center the touchArea to the View*/
        return CGPoint(touchPos.x * touchArea.width,touchPos.y * touchArea.height) + touchAreaPos
    }
}

这是我写的一篇关于 macOS 中的 GestureHUD 类的文章。还包含指向现成扩展的链接:http://eon. codes/blog/2017/03/15/Gesture-HUD/

示例:

example

Swift 3:

I've written an extension to NSTouch that returns the trackpad-touch pos, relative to an NSView:

extension NSTouch {
    /**
     * Returns the relative position of the touch to the view
     * NOTE: the normalizedTouch is the relative location on the trackpad. values range from 0-1. And are y-flipped
     * TODO: debug if the touch area is working with a rect with a green stroke
     */
    func pos(_ view:NSView) -> CGPoint{
        let w = view.frame.size.width
        let h = view.frame.size.height
        let touchPos:CGPoint = CGPoint(self.normalizedPosition.x,1 + (self.normalizedPosition.y * -1))/*flip the touch coordinates*/
        let deviceSize:CGSize = self.deviceSize
        let deviceRatio:CGFloat = deviceSize.width/deviceSize.height/*find the ratio of the device*/
        let viewRatio:CGFloat = w/h
        var touchArea:CGSize = CGSize(w,h)
        /*Uniform-shrink the device to the view frame*/
        if(deviceRatio > viewRatio){/*device is wider than view*/
            touchArea.height = h/viewRatio
            touchArea.width = w
        }else if(deviceRatio < viewRatio){/*view is wider than device*/
            touchArea.height = h
            touchArea.width = w/deviceRatio
        }/*else ratios are the same*/
        let touchAreaPos:CGPoint = CGPoint((w - touchArea.width)/2,(h - touchArea.height)/2)/*we center the touchArea to the View*/
        return CGPoint(touchPos.x * touchArea.width,touchPos.y * touchArea.height) + touchAreaPos
    }
}

Here is an article I wrote about my GestureHUD class in macOS. With link to a ready-made extension as well: http://eon.codes/blog/2017/03/15/Gesture-HUD/

Example:

example

醉酒的小男人 2024-09-23 05:42:59

我不知道是否有 ObjC 接口,但您可能会找到 C HID 类设备接口 有趣。

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

弥繁 2024-09-23 05:42:59

在 Cocoa(Obj-C 级别)尝试以下操作 - 尽管请记住许多用户仍在使用鼠标控制。

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文