3

My web app has a feature where the user can hold Ctrl (Windows, ctrlKey) or Cmd (Mac, metaKey). I want to provide help text telling the user they can do this.

What's a reliable way to detect a Mac-like keyboard? Is navigator.platform.match(/mac/i) safe enough?

Steve Bennett
  • 114,604
  • 39
  • 168
  • 219
  • What about `hold Ctrl (Windows) or Cmd (Mac)` as help text? – marekful Feb 12 '18 at 02:26
  • 1
    Possible duplicate of [How to detect my browser version and operating system using JavaScript?](https://stackoverflow.com/questions/11219582/how-to-detect-my-browser-version-and-operating-system-using-javascript) – Vasyl Moskalov Feb 12 '18 at 02:40
  • 1
    @marekful That's a starting point, but I don't think it's great UX to provide irrelevant information to users when you can do better. – Steve Bennett Feb 12 '18 at 22:02
  • The Apple command key is a non standard modifier and as such is implemented with different keycode values in different browsers. You can however detect OS from User-Agent. – marekful Feb 13 '18 at 04:49

1 Answers1

2

This seems to be reliable:

const isMac = navigator.userAgent.includes('Mac');
Steve Bennett
  • 114,604
  • 39
  • 168
  • 219