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

130
Views
Document.addEventListener() keyup and keydown don't work after click

I'm writing a tampermonkey script that I want to perform an action when a certain set of keys is pressed (cmd + alt + t). I'm seeing a problem where the code stops executing altogether after I click anywhere on the page and I have to either hard refresh or click a certain place on the page (search bar in CloudWatch) to get it working again.

Here's my code:

(async function() {
    var set = new Set();

    function funcA() {
        console.log('funcA');
    }

    function funcB() {
        console.log('funcB');
        if (set.has(91) && set.has(18) && set.has(84)) { // cmd + alt + t
            funcA();
        }
    }

    function onKeyUp(e) {
        console.log('onKeyUp');
        set.clear();

        console.log(new Array(...set).join(' '));
    }

    function onKeyDown(e) {
        console.log('onKeyDown');
        set.add(e.which);

        console.log(new Array(...set).join(' '));

        funcB();
    }

    function init() {
        console.log('init');

        console.log('Adding event listeners.');
        document.addEventListener('keyup', onKeyUp);
        document.addEventListener('keydown', onKeyDown);
    }


    if( document.readyState !== 'loading' ) {
        console.log( 'not loading' );
        init();
    } else {
        document.addEventListener('DOMContentLoaded', init);
    }
})();

The problem only seems to happen on certain pages, for example in AWS CloudWatch logs.

Any ideas?

about 4 years ago · Juan Pablo Isaza
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!