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

92
Views
how do i track key presses and log them to the console in Javascript

lately, I've started making a very simple program that I what to basically be a bunch of hotkeys for different JavaScript tags. But I don't know how to track the key presses and log them into the console.

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

0

In HTML

<input type="text" id="username" onkeyup="myFunc()">

In Javascript

function myFunc(){
    console.log(document.getElementByID('username).value)
}
about 4 years ago · Juan Pablo Isaza Report

0

in the whole window (to see the code snippet work, click Run code snippet then click in the blank area below and type some keys :) )

// list of modifiers : https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/getModifierState
var modifiers = ["CapsLock", "Control", "Alt", "Shift", "AltGraph"];
var upOrDown = function(event) {
    var element = document.getElementById("status");
    var key = event.key || event.which || event.keyCode; //find the key that was pressed
    var status = "Pressed :";
    for (const modifier of modifiers) {
        if (event.getModifierState(modifier))
        {
            status += " "+modifier;
        }
    }
    if ("keydown"==event.type && !modifiers.includes(key))
    {

        status += " "+key;
    }
    element.innerHTML = status;
    //console.log(status);
}
window.addEventListener("keydown",upOrDown,false);
window.addEventListener("keyup",upOrDown,false);
<div id="status">Pressed : </div>

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!