0
votes

firstly English isn't my native language, sorry if I have any mistakes.

There is message sending problem in the function, I've played with Jquery codes but I couldn't fix it.

When I press the Enter, message reach to receiver, that's good. But when I press the Shift with Enter, message reach to receiver again, I want to create new line when press the both keys.

Jquery codes:

$(document).ready(function() {
	$('input#chat').bind('keydown', function(e) {
		if(e.keyCode==13) {
			// Store the message into var
			var message = $('input#chat').val();
			var id = $('#chat').attr('class');
			if(message) {
				// Remove chat errors if any
				$('.chat-error').remove();
				
				// Show the progress animation
				$('.message-loader').show();
				
				// Reset the chat input area			
				document.getElementById("chat").style.height = "25px";
				$('input#chat').val('');
2
I've not done much keyboard handling in Javascript, but I'm betting that if you have Enter pressed no matter what else is there, e.keyCode === 13 will be true and thus effectively be the same as pressing enter. An idea is to handle modifiers before this. - jdphenix

2 Answers

0
votes

Did you try it ?

 if(e.shiftKey && e.keyCode==13){
     // Don't fill
 } else if(e.keyCode==13){
     e.preventDefault();
     // Store the message into var
        var message = $('input#chat').val();
        var id = $('#chat').attr('class');
        if(message) {
        ........................
 }
0
votes

In this, while the shift key is pressed a boolean prevents the code from being run.

var ShiftDown = false; //Is false when not being pressed, true when being pressed
$(document).ready(function() {
	$('input#chat').keydown(function(e) {
		if(e.which === 16) {
            //Shift key is pressed
            ShiftDown = true;
        }else if(e.which === 13){
            //Code will only run if Shift key is not pressed
            if(ShiftDown === false){
			    // Store the message into var
			    var message = $('input#chat').val();
			    var id = $('#chat').attr('class');
			    if(message) {
				    // Remove chat errors if any
				    $('.chat-error').remove();
				
				    // Show the progress animation
				    $('.message-loader').show();
				
				    // Reset the chat input area			
				    document.getElementById("chat").style.height = "25px";
                    $('input#chat').val('');
                }
            }
        }
    })
    $('input#chat').keyup(function(e) {
        if(e.which === 16){
            //Shift key is no longer pressed
            ShiftDown = false;
        }
    });
}

I've just changed

$('input#chat').bind('keydown',function(e){}) 

to

$('input#chat').keydown(function(e){})