Bill
Bill

Reputation: 19238

Ctrl + Enter with JavaScript to prevent the enter

I would like to save a message when user press ctrl and enter at the same time, here is my code

if (e.ctrlKey && e.keyCode == 13)
{
    e.preventDefault();
    saveMessage();
}       

<textarea placeholder="Add a note..." id="note-content" ng-model="noteContent" ng-keyup="triggerform($event)"></textarea>

It works however, it would keep create a extra enter at the end of line which is trigger by the enter key, is there a way i can prevent the enter?

Upvotes: 0

Views: 1074

Answers (1)

Krab
Krab

Reputation: 6756

You should use ng-keypress instead of ng-keyup.

keydown vs keyup vs keypress

In order to understand the difference between keydown and keypress, it is useful to understand the difference between a "character" and a "key". A "key" is a physical button on the computer's keyboard while a "character" is a symbol typed by pressing a button. In theory, the keydown and keyup events represent keys being pressed or released, while the keypress event represents a character being typed. The implementation of the theory is not same in all browsers.

Upvotes: 1

Related Questions