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

155
Visualizações
How to handle shift pressed before another key in onkeyup?

I need to handle usb barcode scanner in my web page, all was working well on my computer but when i tried it on another one (Windows 7 and another scanner) i had an issue with shift key.

The scanner barcode has : symbol and when it's read it's passed like Shift; how can i get the real symbol from it?

Here is my function when i handle the scanning:

document.addEventListener("keyup", (event) => {
  let key = event.key;
  if (event.isComposing || key === 'Shift' || key === 'Control' || key === 'Meta' || key === 'Alt') {
    return;
  }
  
  if (containerLoading.classList.contains("d-none")) {
    showLoading();
  }
  
  if (!containerLoading.classList.contains("d-none")) {
     if (key === "Enter" || key === "Tab") {
      if (barcode != "") {
        websocket.send(`<ART>${barcode}</ART>`);
        barcode = "";
      }
    } else {
      barcode += key;
    }
  }
});

Here is how the scanner barcode result in notepad (and that's how it should be) and under you can find how it looks like in the browser:

enter image description here

TEST TEXT:

WRONG

Hc1ò6BFOXN%Ts3DHPVo13j -g'-2YRVa.q-r8'd32Fc1j9M$Di9CPi9ELNLAPS$SçLc-GPWBILc9Ff9

84Ev28JVSAp921TDUMx6WUKAMa800p92ò3

RIGHT

HC1ç6BFOXN%TS3DHPVO13J -G'-2YRVA.Q-R8'D32FC1J9M$DI9CPI9ELNLAPS$SçLC-GPWBILC9FF9

(4EV28JVSAP921TDUMX6WUKAMA800P92ç3

ENG KEYBOARD

HC1:6BFOXN%TS3DHPVO13J /G-/2YRVA.Q/R8-D32FC1J9M$DI9CPI9ELNLAPS$S:LC/GPWBILC9FF9

*4EV28JVSAP921TDUMX6WUKAMA800P92:3
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Solved by replacing keyup with keypress

Now the code looks like this:

document.addEventListener("keypress", (event) => {
  let key = event.key;
  if (event.isComposing || key === 'Shift' || key === 'Control' || key === 'Meta' || key === 'Alt') {
    return;
  }
  
  if (containerLoading.classList.contains("d-none")) {
    showLoading();
  }
  
  if (!containerLoading.classList.contains("d-none")) {
     if (key === "Enter" || key === "Tab") {
      if (barcode != "") {
        websocket.send(`<ART>${barcode}</ART>`);
        barcode = "";
      }
    } else {
      barcode += key;
    }
  }
});
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