alt and two letter shortcut

Asked

Viewed 269 times

0

good afternoon I got that code right here

$(document).on('keydown', function(e) {
  console.log(e.which); // Retorna o número código da tecla
  console.log(e.altKey); // Se o alt foi Pressionado retorna true
  if ((e.altKey) && (e.which === 80)) { // Pesquisar (Alt + P)
    document.body.innerHTML = 'Abriu Pesquisa.';
  }
});

it is alt plus the letter plus in the case need two letter example alt+p+o is possible someone who knows could help me from now on thank

  • Alt+Key+Key is not a unique combination. What happens in desktop applications is something like Alt+Keys1 followed by Alt+Keys2.

  • interesting could give me an example, maybe this can help me

1 answer

0


The key capture can pick up one key at a time and more the state of the shift, ctrl or alt. Then your code can capture the first choice and wait for the second. On the second capture, it performs the action you want.

Illustrative example:

var firstA = false;
var firstB = false;

$(document).on('keydown', function(e) {

  if (e.altKey && e.which === 65) { 
    $(".key-a").css("background-color", "yellow");
    firstA = true;    
  }
  if (e.altKey && e.which === 66) { 
    $(".key-b").css("background-color", "green");
    firstB = true;    
  }
  if (firstA === true && e.altKey && (e.which === 49 || e.which === 97)) {
    $(".key-a-1").css("background-color", "orange");
    firstA = false;
  }
  if (firstA === true && e.altKey && (e.which === 50 || e.which === 98)) {
    $(".key-a-2").css("background-color", "orange");
    firstA = false;
  }
  if (firstB === true && e.altKey && (e.which === 49 || e.which === 97)) {
    $(".key-b-1").css("background-color", "orange");
    firstB = false;
  }
  if (firstB === true && e.altKey && (e.which === 50 || e.which === 98)) {
    $(".key-b-2").css("background-color", "orange");
    firstB = false;
  }
  if (!firstA || !firstB) {
    // limpa tela
    setTimeout(function () { $(".key").css("background-color", "white") }, 2000);
  }
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>Menu</p>
<ul>
  <li class="key key-a">
    <p>Item 1 (ALT + A)</p>
    <ul>
      <li class="key key-a-1">Item 1.1 (ALT + A + 1)</li>
      <li class="key key-a-2">Item 1.2 (ALT + A + 2)</li>
    </ul>
  </li>
  <li class="key key-b">
    <p>Item 2 (ALT + B)</p>
    <ul>
      <li class="key key-b-1">Item 2.1 (ALT + B + 1)</li>
      <li class="key key-b-2">Item 2.2 (ALT + B + 2)</li>
    </ul>
  </li>  
</ul>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.