Prevent default keystrokes in Internet Explorer

I am trying to override Control P in Internet Explorer 10, but cannot figure out how to do this. I mocked Fiddle with very simple code that works in Chrome (at least on my Mac). But running this in IE 10 and using Control P still brings up the print dialog.

Here is my simple code:

$('.test').on('keydown', function(e){ if (e.metaKey || e.ctrlKey){ $('body').append('ctrl p pressed'); e.preventDefault(); return false; } });​ 

Does anyone know what is going on here?

+4
source share
1 answer

To prevent default behavior

For instance. (Prevent the default behavior for Ctrl + O and Ctrl + P)

 /*jslint browser: true */ (function scriptInitScript() { "use strict"; document.attachEvent("onkeydown", function handleKeyDown(event) { if (event.ctrlKey) { switch (event.keyCode) { case 79: // o case 80: // p event.keyCode = 0; return false; } } }); }()); 

Jsfiddle

Note. jQuery 1.9 uses addEventListener , if available. See on github .

+4
source

Source: https://habr.com/ru/post/1446807/


All Articles