How to capture cmd + enter event in jquery

I need to determine when the user presses cmd + enter after entering text in the text box. I am using jquery.

Here is what I tried:

<textarea id="checkMetaKey"></textarea>
<div id="display"></div>

$( "#checkMetaKey" ).keyup(function( event ) {
  $( "#display" ).text(event.metaKey + " " + event.keyCode);
});

JSFiddle: http://jsfiddle.net/WQG2b/

I expect to display "true 13" when pressing cmd + enter. However, it is not.

The only way I can get metaKey to be true is when I only press CMD or CMD + ALT or CMD + CTRL.

How to capture CMD + ENTER event?

This kind of functionality seems to work when I try to use it on Facebook, I can send the text of the message using CMD + ENTER.

+4
source share
1 answer

keydown. event.keyCode == 13

$("#checkMetaKey").keydown(function (event) {
    $("#display").text((event.metaKey || event.ctrlKey) && event.keyCode == 13);
});

DEMO

+14

All Articles