Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

172
Visualizações
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?

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda