Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Comercial
    • Calculadora

0

49
Vistas
pitfalls in detecting undefined keys for keyPress?

I know many people have discussions about this regarding capturing keyPress events for DELETE and BACKSPACE and others. keyPress will not return any charCode or keyCode for DELETE and BACKSPACE.

I have seen some elaborate JS code using event listeners to detect keyPress vs keyDown or keyUP - but all of them really feel like coders are jumping through hoops to resolve what should be a very simple solution.

I came across one users solution that seems rather simple and wanted to expand on it. But it also seems a little too simple so I wanted to ask about any pitfalls this method could unintentionally introduce.

In my template form: ng-keypress="validateKey($event);"

In JS:

$scope.validateKey = function($event) {
  var keyCode = $event.keyCode || $event.charCode ;
  if (keyCode !== undefined) {
     ... do something
  } else {
     return ;
  }
}

This would allow DELETE, BACKSPACE, ALT, CTRL, SHIFT...and probably a handful of others, but its a pretty simple solution to deal with keyPress not registering specific keys yet still allowing to capture everything else that is relevant and that needs to be evaluated. In my case, and probably many cases, allowing those undefined keys to be pressed causes no harm to the form or validation.

But maybe I am not thinking of everything...or not realizing this could cause unforeseen issues...can anyone elaborate on the pitfalls of this method?

7 months ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos