shortcut with alt and two letters

0

Good afternoon I have this code 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.';
  }
});

he and alt plus the letter more in the case of two letter example alt + p + o is possible anyone who knows could help me Thanks in advance.

    
asked by anonymous 05.03.2018 / 16:59

1 answer

0

The key capture can pick up one key at a time, plus the status of shift , ctrl or alt . Then your code can capture the first choice and wait for the second. In 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><liclass="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>
    
05.03.2018 / 17:55