Answered How to disable keyboard shortcuts in IE7 / IE8

  • Wednesday, January 13, 2010 10:29 PM
     
     
    I'm doing some web development using ASP.NET 2.0. I'm using IE 7 and 8 as my browers and I'm trying to scan a complex barcode into the browser using a handheld scanner. The data that comes in via the scanner contains key sequences that translate to keyboard shortcuts so rather than pulling in all of my data correctly, the input of the data launches keyboard shortcuts to add the page to my favorites.

    So my question is whether there is a way to disable all keyboard shortcuts in IE when you open a specific .aspx page. I tried scanning using IE 6 and it worked fine, but not for 7 & 8.

All Replies

  • Wednesday, January 13, 2010 10:42 PM
    Moderator
     
     Answered Has Code
    In JavaScript you can attach and event listener that will handle all key presses and cancel them so they don't bubble up. Something like:

            window.onload = function() {
                window.document.body.onkeydown = function() {
                    var e = window.event; // this only works in IE other browsers pass in the arg rather than use window.event
    
                    if (e.ctrlKey) {
                        e.cancelBubble = true; // ie specific ff is preventDefault()
                        try {
                            e.keyCode = 0; // this is a hack to capture ctrl+f ctrl+p etc
                        }
                        catch (e) {
    
                        }
                        return false;
                    }
    
                    return true; // for keys that weren't shortcuts (e.g. no ctrl) then the event is bubbled
                }
            }
    


    Ideally you should use attachEvent to attach or addEventListener if you're using some cross-browser shim.
  • Friday, February 18, 2011 7:51 PM
     
     
    Backspace means backspace vIP: e.cancelBubble = true; // ie specific ff is preventDefault() Prevent the backspace key bubbling up to the Body and firing the page back.
    Rob^_^