javascript - How do I detect the "enter" key and "shift" key at the same time to insert a line break -
i'm trying create note system. whatever type form gets put div. when user hits enter, submit note. want make when hit shift + enter creates line break point they're typing (like skype). here's code:
$('#inputpnote').keypress(function(event){ var keycode = (event.keycode ? event.keycode : event.which); if(keycode=='13' && event.shiftkey){ $("inputpnote").append("<br>"); } else if(keycode == '13'){ var $pnote = document.getelementbyid("inputpnote").value; if ($pnote.length > 0) { $("#pnotes-list").append("<div class='pnote-list'><li>" + $pnote + "</li></div>"); $('#inputpnote').val(''); } } });
#inputpnote
form user enters note , #pnotes-list
place notes being appended to. thank in advance!
i think you'd have set 2 global variables, 1 shitftkeypress , 1 enterkeypress , you'd need keydown , keyup set values , check see if both true, because logic saying, when key pressed, execute code, if press key , press key, happen function called twice.
edit: example code of should like:
var haspressedshift = false; var haspressedenter = false; $('#inputpnote').keydown(function(event){ if(shiftkey) { haspressedshift = true; } if(enterkey) { haspressedenter = true; } }); $('#inputpnote').keyup(function(event){ if(shiftkey) { haspressedshift = false; } if(enterkey) { haspressedenter = false; } }); $('#inputpnote').keypress(function(event){ if(haspressedshift && haspressedenter) { // } });
this quick mock up, it's similar how should look
Comments
Post a Comment