最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to check if 'enter' key was pressed with 'shift' key in textarea - Stack Overfl

programmeradmin1浏览0评论

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?

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?

Share Improve this question edited Oct 26, 2017 at 16:07 Antoine 8904 gold badges16 silver badges29 bronze badges asked Sep 16, 2015 at 2:04 DevWLDevWL 18.9k6 gold badges97 silver badges92 bronze badges 2
  • developer.mozilla/en-US/docs/Web/API/KeyboardEvent/shiftKey – Dr.Molle Commented Sep 16, 2015 at 2:08
  • 1 e.which could never be both 13 and 16 at the same time :P. You want modifier keys as @Amadan explains quite succinctly below :) – ChevCast Commented Sep 16, 2015 at 2:11
Add a ment  | 

1 Answer 1

Reset to default 9
if (e.which == 13 && e.shiftKey)

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

发布评论

评论列表(0)

  1. 暂无评论