QWebView Disable Backspace Key (But Not on HTML Fields)



  • In QWebView, pressing the backspace key causes the page to go back. How can I disable that key to have the power to do that functionality, while not disabling the backspace key on HTML fields, and not disabling my ability to do history.back() in a Javascript function (as in when I want a Go Back button)?



  • Here's the solution. You have to do it in Javascript. Ensure jQuery is loaded in your web pages (even the IFRAME/FRAME ones) and then apply this code:

    <script type="text/javascript">
    $(document).ready(function(){
    
        // Disable backspace key except in fields.
        $(document).keydown(function(e) {
            var elid = $(document.activeElement).is('INPUT, TEXTAREA') ;
            if (e.keyCode === 8 && !elid) {
                if(e.ctrlKey) {
                    window.history.back();
                } else { // disable backspace
                    e.preventDefault();
                    return false;
                }
            }
        });
    
    });
    </script>
    

    SOURCE: http://stackoverflow.com/a/17278620/105539



Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.