Javascript touch event: distinguishing finger vs. Apple Pencil

拜拜、爱过 提交于 2019-12-19 02:45:08

问题


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


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/34986373/javascript-touch-event-distinguishing-finger-vs-apple-pencil

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!