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

346
Views
How to use JavaScript to get letters pressed on the keyboard?

I need to get only the letters pressed on the keyboard. I've tried to use:

window.addEventListener("keydown", function(event) {
        letter = event.key;
        if(/[a-zA-Z]/.test(letter)) {
            console.log(letter);
        }
        
});

But the console shows, for instance, when "F12", "Shift", "CapsLock", "ArrowRight" and other non-letter keys are pressed. How can i resolve this?

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

0

Please use onkeypress event.

function myKeyPress(e){
  var keynum;

  if(window.event) { // IE                  
    keynum = e.keyCode;
  } else if(e.which){ // Netscape/Firefox/Opera                 
    keynum = e.which;
  }

  alert(String.fromCharCode(keynum));
}
<input type="text" onkeypress="return myKeyPress(event)" />

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!