浏览器中的触摸事件能力实际上并不意味着用户正在使用触摸设备(例如,Modernizr并不能满足要求)。正确回答该问题的代码应该在设备有鼠标时返回false,在没有鼠标的情况下返回true。对于既有鼠标又有触摸的设备,它应该返回false(而不是仅限触摸)。
顺便说一句,我的触摸界面也可能适用于只有键盘的设备,因此我要检测的更多是缺少鼠标。
为了使需求更清晰,这里是我要实现的API:
// Level 1
// The current answers provide a way to do that.
hasTouch();
// Returns true if a mouse is expected.
// Note: as explained by the OP, this is not !hasTouch()
// I don't think we have this in the answers already, that why I offer a bounty
hasMouse();
// Level 2 (I don't think it's possible, but maybe I'm wrong, so why not asking)
// callback is called when the result of "hasTouch()" changes.
listenHasTouchChanges(callback);
// callback is called when the result of "hasMouse()" changes.
listenHasMouseChanges(callback);