3

How can I know macOS command keys in JavaScript.

Need to know left and right command keys. I know key codes of these both but I don't know how to separate from other keys.

tadman
  • 208,517
  • 23
  • 234
  • 262
Shanmugaraja_K
  • 1,914
  • 3
  • 13
  • 25

2 Answers2

3

You could look on keydown/keyup and record when a key is pressed based on event.keyCode.

Or as Alexander pointed out, you can also use:

event.metaKey https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/metaKey

There are some libraries which can help you out as these keys are browser dependent

GibboK
  • 71,848
  • 143
  • 435
  • 658
  • @AlexanderElgin thanks for commenting, please revise my edit. +1 also for your solution which I like and I mention in my answer :) – GibboK Jan 31 '17 at 06:12
2

See the snippet below

$(window).keydown(function (e){
    if (e.metaKey) {
      alert('meta key is pressed');
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Alexander Elgin
  • 6,796
  • 4
  • 40
  • 50