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

179
Visualizações
How to get same ctrl+click behavior across all browsers and platforms

I'm trying to get ctrl + left mouse click to do a certain behavior, and getting different results for different browsers.

Since on mac systems ctrl+lmb is translated to right click, it seems like I have to capture the 'contextmenu' event, and then check if it is actually ctrl+lmb.

But even overcoming that, for the following script, I'm getting different results between chrome and safari.

In Chrome console I see 'Clicked ctrl + left click' on click, and in Safari I see this message twice: while ctrl is held, both when I click lmb and when I release it.

box = document.getElementById("thebox").addEventListener('click',(event) => {
    if (event.ctrlKey) {
        console.log('Clicked ctrl + left click')
        return;  
    }
    console.log('Clicked left click');
})
box = document.getElementById("thebox").addEventListener('contextmenu',(event) => {
    event.preventDefault();
    if (event.buttons === 2 || event.buttons === 0) {
        console.log("Clicked right click");
    } else if (event.buttons === 1) {
        console.log("Clicked ctrl + left click");
    }
})
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <div id="thebox" style='height: 100px; width: 100px; background-color: red;'></div>
    <script src='./index.js'></script>
</body>
</html>

I feel there should be a more simple solution to have a unified ctrl+lmb click across all browsers but can't find it, is it really that difficult?

Appreciate the help.

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