0

I'm trying to create a note system. Whatever you type into the form gets put into a div. When the user hits Enter, they submit the note. However I want to make it so when they hit Shift + Enter it creates a line break a the point where they're typing (like skype). Here's my 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 is the form where the user enters their note and #pnotes-list is the place where the notes are being appended to. Thank you in advance!

Brett DeWoody
  • 59,771
  • 29
  • 135
  • 184
Curtis Chong
  • 783
  • 2
  • 13
  • 26
  • possible duplicate of [How do I detect "shift+enter" and generate a new line in Textarea?](http://stackoverflow.com/questions/6014702/how-do-i-detect-shiftenter-and-generate-a-new-line-in-textarea) – Bram Vanroy Mar 16 '15 at 21:55
  • yes someone here fixed my syntax error which helped detect the shift and enter. In that case it is a duplicate of the other forum. However now i need a way to insert a "linebreak" in the place where someone is typing. I believe that inserting a
    in my code is wrong. Nice job finding that forum though.
    – Curtis Chong Mar 16 '15 at 23:22
  • This question has been asked before, have you checked out http://stackoverflow.com/questions/6014702/how-do-i-detect-shiftenter-and-generate-a-new-line-in-textarea ? – Max Krog Mar 16 '15 at 21:44

1 Answers1

2

I think for this you'd have to set two global variables, 1 for shitftKeyPress and 1 for enterKeyPress and then you'd need a keydown and a keyup to set those values and then you check to see if they are both true, because your logic is saying, when a key is pressed, execute this code, if you press a key and then press another key, the only that will happen is the function will be called twice.

EDIT: Example code of what it should look 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) {
        // Do something
    }
});

This was a quick mock up, but it's similar to how it should look