简体   繁体   English

使用 Javascript 检测操作系统的可靠方法是什么?

[英]What is the reliable way to detect OS using Javascript?

The use of navigator.userAgent is not advised as per MDN.根据 MDN,不建议使用navigator.userAgent In my application I want to have a shortcut for del key.在我的应用程序中,我想要一个 del 键的快捷方式。

In mac del key (is backspace) with charCode = 8. In windows del key charCode = 46.在 mac del 键(是退格键)中,charCode = 8。在 windows del 键中,charCode = 46。

I want to treat both keyEvents same.我想对待两个 keyEvents 相同。 I am currently using userAgent but it is unreliable as that property can be spoofed.我目前正在使用 userAgent,但它不可靠,因为该属性可以被欺骗。 I am wondering what is the best way to know the client OS otherwise.我想知道否则了解客户端操作系统的最佳方式是什么。

More deprecated navigator properties navigator.appVersion navigator.platform更多弃用的导航器属性navigator.appVersion navigator.platform

/* code objective */
if ((keycode == 8 && os == 'macintosh') || keycode == 46) {
    //This is keyboard shortcut to perform delete
}

Relying on KeyboardEvent.metaKey and KeyboardEvent.Backspace solved my problem依靠 KeyboardEvent.metaKey 和 KeyboardEvent.Backspace 解决了我的问题

Full Key Values - https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values完整的键值 - https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values

For more details refer: https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key#Key_values有关更多详细信息,请参阅: https : //developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key#Key_values

Thanks to @Teoli2003 for twitter reply - https://twitter.com/Teoli2003/status/663995373766221824感谢@Teoli2003 的 twitter 回复 - https://twitter.com/Teoli2003/status/663995373766221824

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有没有一种可靠的方法来检测所有设备上的滚动,使用javascript / jQuery? - Is there a reliable way to detect scrolling, on all devices, using javascript/jQuery? 使用Modernizr检测移动设备的最可靠方法是什么? - What is the most reliable way to detect a mobile device using Modernizr? 检测客户端浏览器及其版本号的可靠方法是什么? - What is a RELIABLE way to detect a client's browser and its version number? 使用jQuery / JavaScript查找XML根元素名称的可靠方法是什么? - What is reliable way to find XML root element name using jQuery/JavaScript? 在JavaScript中隐藏/欺骗引用者的最可靠方法是什么? - What is the most reliable way to hide / spoof the referrer in JavaScript? 检查JavaScript变量是否为空的最可靠方法是什么? - What's the most reliable way to check if a JavaScript variable is null? 使用 JavaScript 在设备上检测视网膜支持的最佳方法是什么? - What is the best way to detect retina support on a device using JavaScript? 使用JavaScript检测Internet Explorer 6的最佳方法是什么? - What is the best way to detect Internet Explorer 6 using JavaScript? 使用 JavaScript 检测“触摸屏”设备的最佳方法是什么? - What's the best way to detect a 'touch screen' device using JavaScript? 使用Javascript检测websocket支持的最佳方法是什么? - What is the best way to detect websocket support using Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM