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

166
Views
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
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!