Shanky_Richer
Shanky_Richer

Reputation: 223

How to call method on Browser shortcut keys?

I am working on a project on HTML and javascript. On press Ctrl+P browser will print the web page (which it is displaying).To block this i am using

 e.preventDefault();

but when i tried to call my function after this, Printing of browser get enabled. I want to block the browser printing pop-up and want to display my pop-up box. How can i achieve it?

Upvotes: 0

Views: 299

Answers (1)

This should work (tested in Chrome, Safari and Firefox) (jsfiddle):

$(window).keydown(function(evt){ 
    if((evt.which == "80" && ( evt.metaKey || evt.ctrlKey ))){  
            evt.preventDefault(); 
            alert('not printing anything here');
    }  
});​

Further resources: http://h43z.blogspot.de/2012/11/whats-real-and-whats-not.html, http://labs.neohapsis.com/2012/11/14/browser-event-hijacking/, http://arstechnica.com/security/2012/12/how-script-kiddies-can-hijack-your-browser-to-steal-your-password/

edit: a no jQuery version:

document.addEventListener('keydown',function(evt){
    if((evt.which == "80" && ( evt.metaKey || evt.ctrlKey ))){  
            evt.preventDefault(); 
            alert('not printing anything here');
    }  
});

Upvotes: 1

Related Questions