javascript detect desktop touch and mouse support
Asked Answered
B

1

3

How should touch-enabled devices be differentiates between pure touch and ones that also have a mouse? (like some of today's laptops)

There is a need to give all mouse functionality a priority in an application, and if the device running the app has only touch support, to change the functionality.

If a touch-device also has a mouse pointer, logic suggests the app should consider that device as a normal desktop, and might add touch support as well, but the styling itself should respond to mouse events.

normal touch detection is achieved like so: 'ontouchend' in document;

One way to detect if the computer is desktop might be to check the screen resolution:

window.screen.width >= 1280 // desktop 

but that is not so good because some devices might have very large viewport resolution, and some laptops might have low resolution...

Belloc answered 19/2, 2014 at 17:28 Comment(2)
this post is related: https://mcmap.net/q/42229/-detecting-that-the-browser-has-no-mouse-and-is-touch-only/104380Belloc
duplicate of #21054626Outfield
B
5

After some research, I have found that the best solution was to use this code:

https://github.com/matthewhudson/device.js

So, the way it works, is by giving the body element class names which represent the device and orientation, and with that I can pretty much guess if the user has a mouse. (class desktop is applied)

Belloc answered 7/4, 2014 at 19:48 Comment(6)
what about touch screen desktop,laptop monitorsPassover
what about them? try and seeBelloc
lets say you detect mouse and enhance ui for mouse, but user is using monitor for navigating, and wanted it to work like on tablet.Passover
you need to have 3 modes, one for only mouse, one for only touch and one for both, which is the most difficult to create. Do you have a more specific question?Belloc
yea lets say i want vertical navigation menu to move opposite of mouse, as in mac, to assist user reach their target. But if user is using touch then i just want them to drag menu up and down. Now i have seen a new feature that's not touch, click or drag. It's hover. In some samsung phones. This is akin to moving mouse...lets ignore that for nowPassover
I don't understand what is your question? implementation of use-cases is irrelevant to this question. It's about detection of device capabilities only.Belloc

© 2022 - 2024 — McMap. All rights reserved.