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

294
Visualizações
Add a special character on keypress not working

I would like to add an accented character – á – whenever we use a key combination of Alt + 'a'. Then the same for é with the combination of Alt + 'e'.

I tried to achieve it with this function, however, the results are far from perfect:

$('input').bind('keypress keydown keyup', function(event) {
    if (event.altKey && event.key == 'a') {
        event.preventDefault();
        this.value += 'á';
    }
});

When I use this key combination, I don't get the desired result, instead, I get the Polish "ą".

However, when I only use event.key == 'a' without the event.aktKey, it works and adds value to the input.

Could you please explain why this happens and provide a solution? Thank you.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

There is no reason within your code or jquery that you would get "ą".

But anyway, why bind to all three events: keypress, keydown and keyup? That will result in multiple characters being added for a single key press.

The following seems to work fine.

$('input').on('keydown', function (event) {
    if (event.altKey && event.key == 'a') {
        event.preventDefault();
        this.value += 'á';
    }
});

console.log('\xE1' === 'á');    // true
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input></input>

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