enguerranws
enguerranws

Reputation: 8233

JS backspace keypress event triggered sometimes, sometimes not

I have this piece of code :

container.unbind('keypress').bind('keypress',function (e) {

            var code = e.which,
                charac = String.fromCharCode(code),
                totalChar = $(this).text().replace(/\s+/g, ''),
                diff,
                previousChar = publisher.typeHistory.length > 0 ? publisher.typeHistory[publisher.typeHistory.length-1]:''
                previousCharCode = previousChar.charCodeAt(0);

            if(e.keyCode === undefined){ // backspace
                getCharacterDiff();
                console.log(totalChar.length-1);
                if(totalChar.length-1 === 0){
                    publisher.typeHistory = [];
                }
            }  

            console.log(previousChar);
            if(isCharacterKeyPress(code)){
                if (charac.toUpperCase() != charac.toLowerCase() ) { // Is a char or space


                    if(charac === charac.toUpperCase()){ // Is a char upper ?

                        if(previousChar === ' '){
                            console.log('upper + previous == space ', publisher.typeHistory);
                        }else {
                            publisher.typeHistory = [];
                        }

                        push = true;
                    }
                    else if (charac === charac.toLowerCase()){
                        if(previousCharCode === 32){
                            publisher.typeHistory = [];

                        }
                        if(hasUpperCase(publisher.typeHistory.join(''))){
                            push = true;
                        }
                        else {
                            push = false;
                        }

                    }
                    else {
                        push = false;
                    }


                }
                else if (code === 32){
                    console.log('space');
                    console.log(previousCharCode);
                    if (previousCharCode === 32){
                        push = false;
                        publisher.typeHistory = [];
                    }

                    // else if(previousChar === 32) { // is the spacebar ?
                    //  console.log(previousChar);

                    // }
                }
                else { // = it's a number / , / ., etc..
                        console.log('not chara or space');
                        push = false;
                        publisher.typeHistory = [];



                }


                if (push) {
                    publisher.typeHistory.push(charac);
                }

            }

            console.log(publisher.typeHistory);
            previousTotal = publisher.typeHistory.join('');


            if (publisher.typeHistory.length > 2) {
                exp = publisher.typeHistory.join('');
                publisher.getResults(service,type,exp);
            }
            else {
                if(publisher.typeHistory.length === 0){
                    publisher.typeHistory = [];

                }
                publisher.hideResults();
            }

    });

With this, I handle which keys are typed in elements that have contenteditable=true, then I send it to a method that makes a request to a PHP script.

Here, container can be any element with contenteditable=true.

The problem is : I use e.keyCode === undefined to check if the user type backspace, so I can update the request. But e.keyCode === undefined only works on a single element, not the others (on the other the keypress event is not triggered).

So I don't know where this comes from :

What do I miss ?

Upvotes: 0

Views: 252

Answers (1)

nils
nils

Reputation: 27194

From the MDN on the keypress event:

The keypress event is fired when a key is pressed down and that key normally produces a character value

So you have to use keyup or keydown events for certain keys, such as escape or backspace, because they don't produce characters.

The keycode for backspace is 8, so your code could look like follows:

container.unbind('keyup').bind('keyup',function (e) {

    ...

    if(e.keyCode === 8){ // backspace
        getCharacterDiff();
        console.log(totalChar.length-1);
        if(totalChar.length-1 === 0){
            publisher.typeHistory = [];
        }
    }  

    ...

});

Upvotes: 2

Related Questions