最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Firefox keydown backspace issue - Stack Overflow

programmeradmin0浏览0评论

I'm building a terminal emulation and running into an issue with capturing backspace in Firefox. I'm able to nab the first backspace and remove the last character on the input at the prompt, but it won't persist and remove more than one character.

Actual website: /

Replication here: /

JavaScript code

function handleKeys(e){
    var evt = e || window.event;
    var key = evt.charCode || evt.keyCode;
    if(evt.type == "keydown")
    {
        curr_key = key;
        if(key == 8)
        {
            evt.preventDefault();
            if(0 < $('body').text().length)
                $('body').text($('body').text().slice(0,-1));
        }
    }
    else if(evt.type == "keypress")
    {
        if(97 <= key && key <= 122)
        {
            if(curr_key != key)
                $('body').append(String.fromCharCode(key));
        }
        else
            $('body').append(String.fromCharCode(key));
    }
}
$(function(){
    $('html').live({
        keydown:function(e){
            handleKeys(e);
        },
        keypress:function(e){
            handleKeys(e);
        }
    })
})​

I'm building a terminal emulation and running into an issue with capturing backspace in Firefox. I'm able to nab the first backspace and remove the last character on the input at the prompt, but it won't persist and remove more than one character.

Actual website: http://term.qt.io/

Replication here: http://jsfiddle/BgtsE/1/

JavaScript code

function handleKeys(e){
    var evt = e || window.event;
    var key = evt.charCode || evt.keyCode;
    if(evt.type == "keydown")
    {
        curr_key = key;
        if(key == 8)
        {
            evt.preventDefault();
            if(0 < $('body').text().length)
                $('body').text($('body').text().slice(0,-1));
        }
    }
    else if(evt.type == "keypress")
    {
        if(97 <= key && key <= 122)
        {
            if(curr_key != key)
                $('body').append(String.fromCharCode(key));
        }
        else
            $('body').append(String.fromCharCode(key));
    }
}
$(function(){
    $('html').live({
        keydown:function(e){
            handleKeys(e);
        },
        keypress:function(e){
            handleKeys(e);
        }
    })
})​
Share Improve this question edited Dec 27, 2012 at 6:38 Zack Zatkin-Gold asked Dec 27, 2012 at 5:59 Zack Zatkin-GoldZack Zatkin-Gold 82410 silver badges29 bronze badges 7
  • FYI, live() is deprecated as of jQuery 1.7. You should use bind() or on(). – Barmar Commented Dec 27, 2012 at 6:16
  • Your fiddle works for me. Whether backspace auto-repeats may depend on the OS or browser. I'm using OS X Snow Leopard and Chrome 24. – Barmar Commented Dec 27, 2012 at 6:19
  • 1 Using Firefox on Windows 17.0.1 some bug in slice. I will post more detail in the "answer" but it seems like some sort of bug – HMR Commented Dec 27, 2012 at 6:33
  • @Barmar: Yeah, it's a Firefox-specific bug. – Zack Zatkin-Gold Commented Dec 27, 2012 at 6:38
  • Workaround is to implement your own auto-repeat. keydown handler calls setInterval() to repeat the action periodically, keyup handler cancels it. – Barmar Commented Dec 27, 2012 at 6:42
 |  Show 2 more ments

4 Answers 4

Reset to default 5

Try this: http://jsfiddle/NBZG8/1/

You'll need to handle backspace in both keydown and keypress to support Chrome and Firefox

function handleKeys(e){
    var evt = e || window.event;
    var key = evt.charCode || evt.keyCode;

    if (evt.type == "keydown") {
        curr_key = key;
        if(key == 8 && !$.browser.mozilla) {
            backspaceHandler(evt);
        }
    } else if (evt.type == "keypress") {
        if (key == 8) {
            backspaceHandler(evt);
        } else if (97 <= key && key <= 122) {
            if(curr_key != key) {
                $('body').append(String.fromCharCode(key));
            }
        } else {
            $('body').append(String.fromCharCode(key));
        }
    }
}

function backspaceHandler(evt) {
    evt.preventDefault();
    if(0 < $('body').text().length) {
        $('body').text($('body').text().slice(0,-1));
    }  
};

$(function(){
    $('html').live({
        keydown : handleKeys,
        keypress : handleKeys
    })
})​

In firefox Windows 17.0.1 any value returned by $("selector").text() has an added new line character appended to the end. So the substring didn't work for me:

<html>
    <head>
        <title>test</title>
        <script src="jquery.js"></script>
        <script>
            $("document").ready(function(){
                console.log("body text seems to have a new line character");
                console.log(($('body').text()[5]=="\n"));
            });

            function handleKeys(e){
                var evt = e || window.event;
                var key = evt.charCode || evt.keyCode;
                if(evt.type == "keydown")
                {
                    curr_key = key;
                    if(key == 8)
                    {
                        evt.preventDefault();
                        if(0 < $('body').text().length)
                            // next line works, you might trim the \n if it's there at the end
                            //$('body').text($('body').text().slice(0,-2));
                            // this one didn't work for me
                             $('body').text($('body').text().substring(0,$('body').text().length-1)); 
                    }
                }
                else if(evt.type == "keypress")
                {
                    if(97 <= key && key <= 122)
                    {
                        if(curr_key != key)
                            $('body').append(String.fromCharCode(key));
                    }
                    else
                        $('body').append(String.fromCharCode(key));
                }
            }
            $(function(){
                $('html').live({
                    keydown:function(e){
                        handleKeys(e);
                    },
                    keypress:function(e){
                        handleKeys(e);
                    }
                })
            })
        </script>
    </head>
    <body>12345</body>
</html>

I had the same issue with keypress on mozilla. Thanks to this subject it solves my problem so I'll post my code if anyone try to do the same thing as me.
In my exemple I try to auto space when the user type two numbers, and it didn't work in Firefox so that's my code :

$(function() {

    $('#field1, #field2').on('keypress',function(event) {
        event = event || window.event;
        var charCode = event.keyCode || event.which,
            lgstring = $(this).val().length,
            trimstring;
        if(charCode === 8) {
            event.returnValue = false;
            if(event.preventDefault)
                event.preventDefault();
            if(0 < $(this).val().length) {
                $(this).val($(this).val().slice(0,-1));
            }  
        }
        else if(((charCode > 31) && (charCode < 48 || charCode > 57)) || lgstring >= 14) {
            event.returnValue = false;
            if(event.preventDefault)
                event.preventDefault();
        }
        else {
            trimstring = $(this).val().replace(/ /g,"");
            if((lgstring !== 0) && (trimstring.length % 2) === 0 ) {
                $(this).val($(this).val() + ' ');
            }
        }
    });

});

I noticed that Mozilla handle the backspace as a keypress where Chrome don't.

Sorry for my English I'm French

$('#id').keypress(function(e) {

if(e.charCode > 0 || e.keyCode === 8){
 if(e.keyCode === 8){
   return true;
 }else if((e.charCode !== 0) && ((e.charCode  > 57 && e.charCode  < 65)){
   return false;
   }
 }else if((e.keyCode !== 0) && ((e.keyCode  > 57 && e.keyCode  < 65)){
  return false;
 }
});
发布评论

评论列表(0)

  1. 暂无评论