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

152
Visualizações
Keydown Event Listener does not listen

I'm making a virtual keyboard, where by pressing a physical key, its replica must highlight on the screen. The thing is, that absolutely nothing happens, when I hit the button. The program listens to the mouse event, this part is fine. Code extraction:

_createKeys() {
        const fragment = document.createDocumentFragment();
        const layout = [
            '`', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', 'backspace',
            'tab', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '[', ']',
            'caps lock', 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ';', '\'', '\\', 'enter',
            'shiftLeft', '\\', 'Z', 'X', 'C', 'V', 'B', 'N', 'M', ',', '.', '/', 'shiftRight', 'arrowUp',
            'Ctrl', 'Win', 'Alt', 'space', 'Alt', 'Ctrl', 'arrowLeft', 'arrowDown', 'arrowRight'
        ];

        const createIconHTML = (icon_name) => {
            return `<i class="material-icons">${icon_name}</i>`;
        };

        layout.forEach(key => {
            const keyElement = document.createElement('button');
            const lineBreak = ['backspace', ']', 'enter', 'arrowUp'].indexOf(key) !== -1;
            keyElement.setAttribute('type', 'button');
            keyElement.classList.add('key');
            switch(key) {
                case 'caps lock':
                    keyElement.classList.add('key-wide');
                    keyElement.textContent = 'CapsLock';
                    keyElement.addEventListener("click", () => {
                        this._toggleCapsLock();
                        keyElement.classList.toggle('key-caps', this.properties.capsLock);
                    });
                    keyElement.addEventListener('keydown', (e) => {
                        if(e.key === 'CapsLock') console.log('pressed');
                    });
             }
            fragment.appendChild(keyElement);
            if (lineBreak) {
                fragment.appendChild(document.createElement('br'));
            }
        });
        return fragment;
    },
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You must add the keydown event listener to the document not keyElement otherwise the event will only fire if the element has the focus, which it definitely won't if the cursor is in a text field somewhere else in the page.

Instead of :

keyElement.addEventListener('keydown', (e) => {
    if(e.key === 'CapsLock') console.log('pressed');
});

Use :

document.addEventListener('keydown', (e) => {
    if(e.key === 'CapsLock') console.log('pressed');
});

However, adding ~100+ event listeners to document would not be a good idea. You should create a single event listener on document that listens for all keydown events and activates the appropriate virtual key.

layout.forEach(key => {
  ...
  keyElement.dataset.key = encodeURIComponent(key);
  ...
});

document.addEventListener('keydown', event => {
  let k = encodeURIComponent(event.key);
  let vkey = document.querySelector(`[data-key="${k}"]`);
  vkey?.classList.add('pressed');
});
about 4 years ago · Juan Pablo Isaza Relatório

0

you only call the addEventListener inside the switch case also to document not the element, which happens only with caps, so this needs to get out. also, since it's only one switch case, why won't you use if condition instead? would make the code more readable for you.

about 4 years ago · Juan Pablo Isaza Relatório
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