Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

248
Vistas
How do I make a shortcut key WITHOUT getting input from text fields

I am quiet new to Javascript and so I decided to make a shortcut key userscript for m.youtube. It does work but when I am using that shortcut(Ctrl+Z to undo text) in a text field for writing a comment this happens

Alert popups when using the shortcut in a textfield

I could not find any help with a google search or trying find it on forums, I could be using the wrong terms for searching it.


Current Code

$(function(){
    document.onkeydown = function(evt) {
        evt = evt || window.event;
        if (evt.ctrlKey && evt.keyCode == 90) {
            alert("Ctrl-Z");
        }
    };
});

Goal: I don't want the shortcut to run in a textfield when I press the shortcut key.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can check if event target is type of input. Try this:

$(function() {
    document.onkeydown = function(evt) {
        evt = evt || window.event;

        if (evt.ctrlKey && evt.keyCode == 90) {
            if (!(evt.target instanceof HTMLTextAreaElement) && !(evt.target instanceof HTMLInputElement)) {
                alert("Ctrl-Z");
            }
        }
    };
});

function check () {
        document.onkeydown = function(evt) {
            evt = evt || window.event;
            console.log(evt.target)
            if (evt.ctrlKey && evt.keyCode == 90) {
                if (!(evt.target instanceof HTMLTextAreaElement) && !(evt.target instanceof HTMLInputElement)) {
                    alert("Ctrl-Z");
                }
            }
        };
    }
    
    check()
<textarea id="w3review" name="w3review" rows="4" cols="50">
  hello
  </textarea>
  <input type="text" >

If the event target is not of type input you do alert("Ctrl-Z");

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda