Ctrl+Enter jQuery in TEXTAREA
Actually this one does the trick and works in all browsers:
if ((event.keyCode == 10 || event.keyCode == 13) && event.ctrlKey)
link to js fiddle.
Notes:
- In Chrome on Windows and Linux, enter would be registered as
keyCode
10, not 13 (bug report). So we need to check for either. ctrlKey
is control on Windows, Linux and macOS (not command). See alsometaKey
.
You can use the event.ctrlKey
flag to see if the Ctrl key is pressed, something like this:
$('#textareaId').keydown(function (e) {
if (e.ctrlKey && e.keyCode == 13) {
// Ctrl-Enter pressed
}
});
Check the above snippet here.
Universal solution
This supports macOS as well: both Ctrl+Enter and ⌘ Command+Enter will be accepted.
if ((e.ctrlKey || e.metaKey) && (e.keyCode == 13 || e.keyCode == 10)) {
// do something
}