0

I am looking to detect cmd+c in the keyUp event in Chrome OSX. I need a plain Javascript solution not relaying on external libraries.

I found this How does one capture a Mac's command key via JavaScript? but there is no clear solution to my requirements above..

If this can't be done, second best is to detect it in keyDown Chrome OSX.

Any help?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
kofifus
  • 17,260
  • 17
  • 99
  • 173

1 Answers1

1

If you're not specifically looking for full coverage of + any you could cheat a little bit and use the ClipboardEvent API. MDN flags it as "experimental" but it seems to be supported by all major browsers - in any case it works in Chrome on Mac if that's what you're targeting.

Instead of listening for the keyup event you could just listen for copy:

var el = document.getElementById('myElement');
el.addEventListener('copy', function(evt){
    console.log('Cmd+C pressed');
});

The caveat being: this will also fire on any event that alters the clipboard - such as selecting copy from a right-click context menu (though this may equally be desirable depending on your intent).

Emissary
  • 9,954
  • 8
  • 54
  • 65