Tell me how to bind the event of pressing the button to the button on the keyboard?

<button id="buttonForF12"> Перейти к F12</button> 

According to the results, I want to get such an implementation, so that when I click on the web element with the mouse, the same thing happens if I pressed "F12" on the PC keyboard.

  • one
  • one
    want to understand - write to me, correct the question, but do not look for the minus - qwabra
  • one
    The browser can intercept keystroke events. But he will not allow you to generate keystrokes and forward them to the OS. It's not safe. With this feature, you can delete data from the disk by pressing keys, for example. - ArchDemon pm
  • one
    @Yiulia, do you write an extension for chrome? if not, then it is impossible - qwabra
  • one
    @Yiulia, bugs.chromium.org/p/chromium/issues/detail?id=112277 "We only allow explicit devtools opening." - apparently, and in expansion it is not available. - qwabra

1 answer 1

Unfortunately, this does not work for Javascript security purposes, although if after clicking, an event inside the application (site) should be executed, then there is a solution to bind to one function.

 function toggleBg() { alert("test"); } document.addEventListener('keypress', function(event){ if (event.keyCode == 112) { toggleBg(); } }); 
 <button onclick="toggleBg()">Нажмите здесь или на F1 что-бы запустить тестовую функцию</button>