4
Currently I use the keypress approach to capture the Alt pressed and the key that the user wants, however using the Pressed alt can cause compatibility problems in some browsers.
For example, opening the search window:
$(document).on('keypress', function(e) {
console.log(e.which); // Retorna o número código da tecla
console.log(e.altKey); // Se o alt foi Pressionado retorna true
if ((e.altKey) && (e.which === 112)) { // Pesquisar (Alt + P)
document.write('Abriu Pesquisa.');
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
This action in Firefox will work normally, but if it is used in Chrome it will open the print screen.
So the question is as follows, how can I create shortcuts that are safe independent of the browser used?
This is really tricky. The
Alt
is also used as hotkey for screen readers for visually impaired, among other features. Just one comment on the subject.– Pedro Camara Junior
Is this what you’re looking for? -> http://jsfiddle.net/Sergio_fiddle/vexcuhu2/
– Sergio
@Pedrocamarajunior, of course, I have knowledge about this, I asked why there can be a better approach than this that avoids these problems :)
– Gabriel Rodrigues
@Sergio exactly that :) so the magic should happen in keydown and not keypress?
– Gabriel Rodrigues
@Highlander yes, and using the
80
. I think that’s cross-browser...– Sergio