• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

234
Vistas
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 3 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

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 3 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 Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda