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

281
Views
JS keydown + keypress + preventDefault

What we have:

  1. keydown event + preventDefault prevents keypress event (proof: https://stackoverflow.com/a/57401334/9398364)
  2. stopPropagation doesn't help at all
  3. We can't simulate keypress with dispatchEvent because we don't know full list of unprintable event.key values (which we can't just throw as unicode value using event.key.codePointAt(0))

What we need:

  1. Disable default browser keys reaction (i.e. tab press in chrome) without huge switch expression

  2. Detect keydown events because we need shift, ctrl and other keys events (event.code matters)

  3. Detect keypress events because we need to input unicode characters (event.key matters) without input event and without checking if it is unprintable character (such as shift and ctrl)

How?

UPD: Seems like non-unicode event.key has >1 length (source: https://stackoverflow.com/a/70401792/9398364) Is there any proof?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Solved like this:

    window.addEventListener('keydown', function (event)
        {
            onKeyboard(event.code);
            if (event.key.length === 1 && !event.ctrlKey && !event.metaKey)
            {
                const utf16Code = event.key.codePointAt(0);
                onKeyboardInput(utf16Code);
            }
            event.preventDefault();
        }, false);
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!