开发者

How to catch enter keypress on textarea but not shift+enter? [duplicate]

This question already has answers here: How do I detect "shift+enter" and generate a new line in Textarea? (19 answers) Closed 9 years ago.

I'm doing it 开发者_运维技巧for texarea. A function should be called when the user press Enter, but nothing should be done when Shift + Enter be pressed.

I try to simulate here a feature that many IM communicators have: sending a message on Enter but also adding multiple lines using Shift + Enter.


Test for the enter keycode (13) and if shift key was pressed.

...onkeyup = function(e) {
    if (e.keyCode == 13)
    {
//      if (e.shiftKey === true)
        if (e.shiftKey)  // thruthy
        {
            // new line
        }
        else
        {
            // run your function
        }
        return false;
    }
}

Edit: Accept all truthy values of e.shiftKey


element.onkeydown = function(o) {
  o = o || event;
  if (o.shiftKey && o.keyCode == 13) {
    /* shift + enter pressed */
  }
}


I believe you need a keydown event as well to check for the SHIFT key, http://www.quirksmode.org/js/keys.html


Look at the shiftKey property of the event object which will tell you if shift was held while the key was pressed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜