Question

When user is typing in a html input text, is it possible to keep the browser from showing its menu when the user press the Alt key deliberately or by mistake

Was it helpful?

Solution 2

here is @Runtis code modified just changed e.keyIdentifier == "Alt" to 18==e.keyCode
http://jsfiddle.net/2dqCD/13/

document.addEventListener("keyup",function(e){    
    if(18 == e.keyCode && e.target.nodeName == "INPUT"){ 
        e.preventDefault ? e.preventDefault() : (e.returnValue=false)
    }
});

OTHER TIPS

This should work for you.

Fiddle: http://jsfiddle.net/2dqCD/

This checks to see if alt was pressed on an input node, in the fiddle, you can see it will still allow the alt key outside of the input box.

Hope this helps.

document.addEventListener("keyup",function(e){    
    if(e.keyIdentifier == "Alt" && e.target.nodeName == "INPUT"){ 
        e.preventDefault ? e.preventDefault() : (e.returnValue=false)
    }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top