Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript touch event: distinguishing finger vs. Apple Pencil

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

like image 504
Ron Avatar asked Jan 25 '16 06:01

Ron


People also ask

Can I use finger instead of Apple Pencil?

Drawing mode is entered by tapping on the drawing tools icon, which looks like a little pencil tip with a circle around it. Once you tap that you will access the drawing tools in Notes app (and Markup) and you're able to draw with your finger.

Does Apple Pencil have touch sensitivity?

Apple doesn't provide a specific pressure sensitivity level for the Apple Pencil. Tilt Sensitivity - Apple Pencil is designed to work like a regular pencil, so if you hold it at an angle and press the side of the tip alongside the ‌iPad‌ for something like shading, it works.

Can you use Apple Pencil for gestures?

By default, Apple Pencil 2 responds to the double-tap gesture by toggling between the current tool and the eraser, but people can set double-tap to toggle between the current and previous tool, show and hide the color picker, or do nothing at all.


2 Answers

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.querySelector('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.

like image 64
gefangenimnetz Avatar answered Sep 20 '22 01:09

gefangenimnetz


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

like image 32
Alessandro Cipolletti Avatar answered Sep 19 '22 01:09

Alessandro Cipolletti