Search code examples
xcodeswiftuirealitykitvisionosreality-composer-pro

VisionOS Development - Detect which hand executed the gesture


Is it possible to detect which hand chirality that executed a gesture?

For example,

    TapGesture(count: 2)
            .targetedToAnyEntity()
            .onEnded { value in
                // if left hand
                    // do something
                // if right hand
                    // do something
            }

Solution

  • I was able to detect hand chirality (kind of) with this hack. However, in order for it to work correctly, only one hand can be seen when this is queried.

    func getFirstTrackedHandChirality() async -> HandAnchor.Chirality? {
        for await update in handTracking.anchorUpdates {
            let handAnchor = update.anchor
            guard handAnchor.isTracked else { continue }
            
            if let fingerTip = handAnchor.handSkeleton?.joint(.indexFingerTip) {
                guard fingerTip.isTracked else { continue }
                return handAnchor.chirality
            }
        }
        return nil
    }