How to detect Ctrl+Enter in textarea using jQuery?

Detecting Ctrl+Enter key combination in a textarea is a common requirement for web applications, especially when implementing features like quick form submission or chat interfaces. Using jQuery, we can easily capture this key combination by listening to keyboard events and checking for the specific key codes along with modifier keys.

Method Using keydown Event

The most effective approach is to use jQuery's keydown event handler to detect when the user presses Ctrl+Enter. We need to check for the Enter key (key code 13) while the Ctrl key is pressed.

Example

Here's a complete example that detects the Ctrl+Enter combination in a textarea −

<!DOCTYPE html>
<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <script>
        $(document).ready(function(){
            $('#myTextarea').keydown(function(e) {
                // Check if Ctrl+Enter is pressed
                if (e.ctrlKey && e.which === 13) {
                    $('#result').text('Ctrl+Enter detected! Form submitted.');
                    e.preventDefault(); // Prevent default behavior
                    return false;
                }
                
                // Display current key information
                var keyInfo = 'Key code = ' + e.which + ' ' + 
                             (e.ctrlKey ? 'Ctrl' : '') + ' ' + 
                             (e.shiftKey ? 'Shift' : '') + ' ' + 
                             (e.altKey ? 'Alt' : '');
                
                $('#keyInfo').text(keyInfo);
            });
        });
    </script>
</head>
<body>
    <h3>Type in the textarea and press Ctrl+Enter:</h3>
    <textarea id="myTextarea" rows="5" cols="50" placeholder="Type here and press Ctrl+Enter..."></textarea>
    
    <p id="result" style="color: green; font-weight: bold;"></p>
    <p id="keyInfo" style="color: blue;"></p>
</body>
</html>

Key Points

The code works by checking two conditions simultaneously −

  • e.ctrlKey − Returns true when the Ctrl key is pressed
  • e.which === 13 − Checks if the Enter key (key code 13) is pressed
  • e.preventDefault() − Prevents the default behavior of adding a new line

Alternative Method

You can also use the more modern e.key property instead of e.which

<!DOCTYPE html>
<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <script>
        $(document).ready(function(){
            $('#modernTextarea').keydown(function(e) {
                if (e.ctrlKey && e.key === 'Enter') {
                    $('#modernResult').text('Ctrl+Enter detected using e.key property!');
                    e.preventDefault();
                    return false;
                }
            });
        });
    </script>
</head>
<body>
    <h3>Modern approach using e.key:</h3>
    <textarea id="modernTextarea" rows="3" cols="50" placeholder="Press Ctrl+Enter here..."></textarea>
    
    <p id="modernResult" style="color: red; font-weight: bold;"></p>
</body>
</html>

This method provides a reliable way to detect the Ctrl+Enter combination in textareas, making it perfect for implementing quick submission features in forms and chat applications.

Updated on: 2026-03-13T20:51:35+05:30

124 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements