Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

241
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!