Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 290
rsk82 Avatar asked May 30 '11 16:05

rsk82


People also ask

How do I detect shift enter and generate a new line in textArea?

To detect shift+enter and generate a new line in text area with JavaScript, we can check for shift and enter key presses in the event object. textArea. onKeyPress = (e) => { if (e. keyCode === 13 && e.

How to detect if enter key is pressed in a text input field using jQuery?

To check if an “enter” key is pressed inside a textbox, just bind the keypress() to the textbox. $('#textbox'). keypress(function(event){ var keycode = (event.

How to check if enter was pressed?

To check whether user pressed ENTER key on webpage or on any input element, you can bind keypress or keydown event to that element or document object itself. Then in bind() function check the keycode of pressed key whether it's value is 13 is not.

How to capture enter key press event in jQuery?

To trigger a click button on ENTER key, We can use any of the keyup(), keydown() and keypress() events of jQuery. keyup(): This event occurs when a keyboard key is released. The method either triggers the keyup event, or to run a function when a keyup event occurs.


2 Answers

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

like image 70
aorcsik Avatar answered Sep 24 '22 04:09

aorcsik


element.onkeydown = function(o) {   o = o || event;   if (o.shiftKey && o.keyCode == 13) {     /* shift + enter pressed */   } } 
like image 42
ic3b3rg Avatar answered Sep 25 '22 04:09

ic3b3rg