Javascript touch event: distinguishing finger vs. Apple Pencil

后端 未结 2 1245
礼貌的吻别
礼貌的吻别 2021-01-04 15:16

In Mobile Safari, is there any way to distinguish whether a touch event is generated by finger or Apple Pencil?

相关标签:
2条回答
  • 2021-01-04 15:59

    The TouchList object of a touch event contains detailed information about the touch’s individual points. Among the many properties touchType is probably the most interesting for you as it contains either stylus (Apple Pencil) or direct (finger).

    var body = document.getElementByTagName('body');
    body.addEventListener('touchstart', function(evt){
        // should be either "stylus" or "direct"
        console.log(evt.touches[0].touchType);
    });
    

    You should also have a look at the other properties of each individual Touch like force or azimuthAngle that can give you detailed information about the touch point’s current device(s).

    Please note that the Touch interface is only part of a W3C working draft and not official standard yet – however works in iOS 10 + Safari + Apple Pencil.

    0 讨论(0)
  • 2021-01-04 16:03

    You can check the touch force with:

    e.touches[0].force;
    

    But it works also for 3DTouch on iPhone 6s.

    Only Apple Pencil events and touches events on iPhone 6s have .force

    0 讨论(0)
提交回复
热议问题