简体   繁体   中英

What is the correct way to determine whether the paste short cut is 'ctrl + v' or 'cmd + v'

I need to show users that to paste they need to do it using keyboard commands. However, the paste command is different on Mac as it is on Windows and I need to be able to detect that. What is the correct way of doing this in JavaScript WITHOUT having to do OS checks (as that can be faked, thus why we no longer do browser checks)

Have you had a look to this question; How does one capture a Mac's command key via JavaScript?

If you detect the window.navigator.platform of the user, it will tell you which platform the user is using. You can read up on how to use it here .

Summarizing, you can detect it like this:

var isMacLike = navigator.platform.match(/(Mac|iPhone|iPod|iPad)/i);
var isIOS = navigator.platform.match(/(iPhone|iPod|iPad)/i);

if(isMacLike || isIOS ){
  //MAC (remove isIos if you dont care about iphone/ipod/ipad)
}
else{ //other O.S.
  //close button on right side
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM