Javascript触摸事件:区分手指与Apple Pencil

Ron*_*Ron 7 javascript mobile-safari touch-event

在Mobile Safari中,有没有办法区分触摸事件是由手指还是Apple Pencil生成的?

gef*_*etz 7

TouchList触摸事件的对象包含有关触摸的各个点的详细信息.在众多属性touchType中,对你来说可能是最有趣的,因为它包含手写笔(Apple Pencil)或direct(手指).

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

您还应该查看每个Touch的其他属性,force或者azimuthAngle可以为您提供有关触摸点当前设备的详细信息.

请注意,Touch界面只是W3C工作草案的一部分,而不是官方标准 - 但适用于iOS 10 + Safari + Apple Pencil.