2

The use of navigator.userAgent is not advised as per MDN. In my application I want to have a shortcut for del key.

In mac del key (is backspace) with charCode = 8. In windows del key charCode = 46.

I want to treat both keyEvents same. I am currently using userAgent but it is unreliable as that property can be spoofed. I am wondering what is the best way to know the client OS otherwise.

More deprecated navigator properties navigator.appVersion navigator.platform

/* code objective */
if ((keycode == 8 && os == 'macintosh') || keycode == 46) {
    //This is keyboard shortcut to perform delete
}
Prabhakar Kasi
  • 531
  • 4
  • 18
  • 2
    Possible duplicate of [How to find the operating system version using JavaScript](http://stackoverflow.com/questions/9514179/how-to-find-the-operating-system-version-using-javascript) – Anderson Green Nov 10 '15 at 00:27
  • Are you only looking for the OS name or you would like to know the platform as well? – Moe Nov 10 '15 at 00:28
  • 1
    https://dmauro.github.io/Keypress/ – Paul Browne Nov 10 '15 at 00:29
  • @Moe - Just the OS name is good enough to differentiate macintosh – Prabhakar Kasi Nov 10 '15 at 00:32
  • Note that keypress itself uses navigator.userAgent https://github.com/dmauro/Keypress/blob/development/keypress.js#L776 – Schahriar SaffarShargh Nov 10 '15 at 00:36
  • @AndersonGreen - navigator properties have been taken out of web standard. So I am looking for other ways. – Prabhakar Kasi Nov 10 '15 at 00:49
  • I don't think you should be worried about userAgent spoofing. The only real concern as the author mentioned is that navigator and almost every property under it has been deprecated from Web standards thus making the use of most code written with it obsolete. – Schahriar SaffarShargh Nov 10 '15 at 00:49
  • @SchahriarSaffarShargh - I understand and agree. I have added my code-objective. Cannot think of a way to achieve that!! – Prabhakar Kasi Nov 10 '15 at 00:55
  • I think this is a legitimate question regarding detecting the delete key (maybe you should've titled it like that) As far as I know there are no new specs for detecting the OS (since the reason why they were deprecated was unreliability, I could be wrong though) but I think the best way to solve this is to see if there is a common way to detect backspace across platforms. – Schahriar SaffarShargh Nov 10 '15 at 00:58
  • @SchahriarSaffarShargh - That's my dev box – Prabhakar Kasi Nov 10 '15 at 01:11
  • Looks like virtual key code is the way to go but browser support? it's going be a long wait until then we are struck with navigator properties. https://twitter.com/Teoli2003/status/663995373766221824 – Prabhakar Kasi Nov 10 '15 at 21:19

1 Answers1

1

Relying on KeyboardEvent.metaKey and KeyboardEvent.Backspace solved my problem

Full 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

Thanks to @Teoli2003 for twitter reply - https://twitter.com/Teoli2003/status/663995373766221824

Prabhakar Kasi
  • 531
  • 4
  • 18