Why fully working code:

document.onkeydown = function hotkeys(e) { if (!e) e = window.event; var k = e.keyCode; if (e.ctrlKey) { if (k == 77) { alert('works'); } // Ctrl + M } }​ 

is not executed in in JSFiddle , but when run from a clean * .html document works correctly.

I have a problem: when implementing this code on a local work site (I am an office plankton, yes) - the behavior is similar to the behavior on JSfiddle, nothing falls to the console, there is practically no programming experience, I don’t imagine how to even google it.

[UPDATE]: Yes, indeed, it is necessary for mice to hang the focus on the display window in jsFidle (lower right). I did not execute the code (there is a self-written extension for G.Chrome, which increases some functionality for the site in LAN at work) I don’t know why, but moving it to the very top of the extension file helped. I am grateful to those who answered, your answers prompted me to a decision :)

  • one
    Opera 12.00, everything works. Try document.onkeydown = function (e) { - SoWa

1 answer 1

It is necessary to transfer the focus of the iframe (click on the bottom right window) in which the JavaScript is executed, then your code should work.

Apparently for security reasons, if the iframe does not have focus, then it simply does not receive keystroke events.