“Atajos de teclado JavaScript” Código de respuesta

Cómo agregar atajos de teclado JavaScript

document.addEventListener("keydown", (event) => {
	/*
		event.key returns the character being pressed
        event.preventDefault() prevents the default browser behavior for that shortcut, e.g. ctrl+r usually reloads the page, but event.preventDefault() will turn off that behavior. event.preventDefault() should only be used if you share a shortcut with the same trigger as the browser.
        event.ctrlKey / event.altKey / event.shiftKey / event.metaKey all return booleans. They will be true when they are being pressed, otherwise they will be false.
	*/
	if (event.ctrlKey && event.key.toLowerCase() === "d") {
    	//Do something
	} else if (event.ctrlKey && event.key.toLowerCase() === "p") {
    	//Do something else
	}
});	
Spen

JS atajo

document.addEventListener('keydown', (e) => {
  if (e.ctrlKey && String.fromCharCode(e.keyCode).toLowerCase() === 's') {
    e.preventDefault();
    e.stopPropagation();
    // Do some stuff...
  }
}, false);
garzj

Atajos de teclado JavaScript

document.onkeyup = function(e) {
    if (e.which == 16) {
        alert('You have press Shift')
    }
    if (e.which == 17 && e.which == 13) {
        alert('You have press CTRL + Enter')
    }
    //Keyboard events codes
    //https://www.toptal.com/developers/keycode
};
Marco Tellez

Respuestas similares a “Atajos de teclado JavaScript”

Preguntas similares a “Atajos de teclado JavaScript”

Más respuestas relacionadas con “Atajos de teclado JavaScript” en JavaScript

Explore las respuestas de código populares por idioma

Explorar otros lenguajes de código