Last active
January 26, 2021 08:23
-
-
Save avesus/3dfe0ed12c6fd4dfb98def25f9fa1d87 to your computer and use it in GitHub Desktop.
Stylus Detection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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