How to catch enter keypress on textarea but not shift+enter? [duplicate]
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.
精彩评论