Skip to content

Instantly share code, notes, and snippets.

@avesus
Last active January 26, 2021 08:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avesus/3dfe0ed12c6fd4dfb98def25f9fa1d87 to your computer and use it in GitHub Desktop.
Save avesus/3dfe0ed12c6fd4dfb98def25f9fa1d87 to your computer and use it in GitHub Desktop.
Stylus Detection
// Apple:
var body = document.getElementByTagName('body')
body.addEventListener('touchstart', function(evt){
// should be either "stylus" or "direct"
console.log(evt.touches[0].touchType)
})
// Chrome & Edge:
if (window.PointerEvent) {
// Pointer events are supported.
}
navigator.maxTouchPoints
// "touch-action: none;" directs all events to JS
somethingCool.addEventListener('pointerdown', (e) => {
switch (e.pointerType) {
case 'mouse':
/* mouse detected */
break
case 'pen':
/* pen / stylus detected */
break
case 'touch':
/* touch detected */
break
default:
/* pointerType unknown or cannot be detected */
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment