Search code examples
javascriptjquerykeypressshiftenter

How to check if 'enter' key was pressed with 'shift' key in textarea


I know that the shift key code is 16, and the enter key code is 13.

//@ catching any 'enter' keypress in textarea.
    function textareaOnEnter(){
        $('textarea#someId').keypress(function(e)
        {
          if(e.which == 13)
          {
            //.. works only for 'enter'
          }
        });     
    }

I thought it could be as simple as this:

    function textareaOnShiftAndEnter(){
        $('textarea#someId').keypress(function(e)
        {
          if(e.which == 13 && e.which == 16)
          {
            //.. don't work for nothing
          }
        });     
    }

But of course it simply doesn't work as I would expected. How do I check for shift + enter key press?


Solution

  • if (e.which == 13 && e.shiftKey)
    

    Shift, Ctrl and Alt are modifier keys, that get their own flags in the event data.