How to reassign hotkeys in Chrome extention using JS?

I have a working code:

document.onkeypress=function(e){   
    if( e.keyCode==49 ){
        e.preventDefault();
        alert('Переход на целевую страницу при нажатии 1');
        window.location.href="http://yandex.ru";
    } 
};

But it doesn't work if I try to assign key like F1, F2, F3 Chrome opens Help (F1) and so on. e.preventDefault(); doesn't work. How can I update my code?

Upvotes: 0

Views: 49

Answers (1)

Nikhil Devre
Nikhil Devre

Reputation: 393

In some browsers keypress doesn't work but keydown works. You may want to reference this: http://www.quirksmode.org/js/keys.html.

I hope this helps.

Upvotes: 1

Related Questions