问题 如何检测设备是否支持鼠标?


我目前使用以下测试(取自Modernizr)来检测触摸支持:

function is_touch_device() {
    var bool;
    if(('ontouchstart' in window) || window.DocumentTouch && document instanceof DocumentTouch) {
        bool = true;
    } else {
        injectElementWithStyles(['@media (',prefixes.join('touch-enabled),('),mod,')','{#modernizr{top:9px;position:absolute}}'].join(''), function(node) {
            bool = node.offsetTop === 9;
        });
    }
    return bool;
}

但有些设备是触摸和鼠标驱动的,所以我想要一个单独的功能来检测设备是否有鼠标支持。有什么好办法做这个检查?

最终我的意图是能够做到这些:

if(is_touch_device())

if(has_mouse_support())

if(is_touch_device() && has_mouse_support())

4321
2018-01-10 20:47


起源

虽然它没有明确地回答你的问题,但我发现这篇文章很有趣,而且还有鼠标支持: html5rocks.com/en/mobile/touchandmouse - John Koerner
未经测试(仅在实际移动鼠标时有效):var mouse = false; window.onmousemove = function(){mouse = true} - Reeno
@Reeno Clever,我认为应该可以工作,但是如果某人有一个已知的解决方案,他会等待一个更加经过测试的答案。编辑:如果鼠标从未移动过,那么它会给出误报。 - TK123
如果有人从不摆动鼠标,他们可能更喜欢其他交互模式...... - dandavis
MHM。根据 html5rocks.com/en/mobile/touchandmouse/#toc-1 我的解决方案不起作用。该死的:)这是一个关于这个主题的长时间讨论: github.com/Modernizr/Modernizr/issues/869 似乎没有一个好的解决方案 - Reeno


答案:


我目前正在使用以下(jQuery),我还没有在特定设备上发现任何缺陷

$(window).bind('mousemove.hasMouse',function(){
    $(window).unbind('.hasMouse');
    agent.hasMouse=true;
}).bind('touchstart.hasMouse',function(){
    $(window).unbind('.hasMouse');
    agent.hasMouse=false;
});

说明:鼠标设备(也是触摸屏笔记本电脑)首先触发鼠标移动,然后才能触发touchstart并将hasMouse设置为TRUE。触摸设备(也就是例如触发鼠标移动的iOS)点击后首先点击触摸开始,然后鼠标移动。那么为什么hasMouse将设置为FALSE。

唯一的问题是这取决于用户交互,只有在鼠标移动或touchstart之后,该值才会正确,因此无法信任在页面加载时使用。


6
2018-02-01 14:58



$(窗口)。一( '鼠标移动',函数(){agent.hasMouse =真})一个( 'touchstart',函数(){agent.hasMouse = FALSE})。 - ariel


如问题评论中所述,特别是 https://github.com/Modernizr/Modernizr/issues/869,还没有好的答案。


2
2018-06-13 16:21



我真的只是把它作为一个答案,以'正式'标记 这个问题 作为副本 - drzaus
如何检查'window.MouseEvent'是否存在 这个评论 建议? - Design by Adrian
来自@DesignbyAdrian的释义 在你链接的那个下方有几条评论:平板电脑的行为不一致,特别是iPad和Surface - drzaus


var clickHandler = (isMouseEventSupported('click') ? 'click' : 'touchstart');

function isMouseEventSupported(eventName) {
    var element = document.createElement('div');
    eventName = 'on' + eventName;
    var isSupported = (eventName in element);
    if (!isSupported) {
       element.setAttribute(eventName, 'return;');
       isSupported = typeof element[eventName] == 'function';
    }
    element = null;
    return isSupported;
}

这是来自我的朋友/同事的代码,他的基础是: http://perfectionkills.com/detecting-event-support-without-browser-sniffing/


1
2018-06-30 15:33



我很确定所有触摸设备都支持onclick事件...这并不意味着他们有鼠标。 - Rudie


CSS媒体 只是为了那个!

您可以通过获取值来检查某些设备是否有鼠标 pointer CSS媒体功能:

if (matchMedia('(pointer:fine)').matches) {
  // Device has a mouse
}

因为它是CSS,你甚至不需要使用JavaScript:

@media (pointer: fine) {
  /* Rules for devices with mouse here */
}

1
2018-06-04 19:13





没有直接的了解方法,您将不得不等待触摸事件或鼠标事件。

假设您想要检测  老鼠 要么 触摸你可以做到以下几点:倾听 touchstart 和 mousemove (后者可以在没有实际鼠标的情况下触摸触摸设备)。无论哪一个首先发射,99%必然会成为你想要的。

这并不考虑实际具有两者的设备。

document.addEventListener('mousemove', onMouseMove, true)
document.addEventListener('touchstart', onTouchStart, true)
function onTouchStart(){
  removeListeners()
  // touch detected: do stuff
}
function onMouseMove(){
  removeListeners()
  // mouse detected: do stuff
}
function removeListeners(){
  document.removeEventListener('mousemove', onMouseMove, true)
  document.removeEventListener('touchstart', onTouchStart, true)
}

0
2018-02-13 22:12