creating javascript html shortcut

Asked

Viewed 591 times

0

Good morning, I put in accesskey="i" to create a shortcut in html, but I need to add two letters, as I do?

<button type="button" class="btn btn-default btnEntidades" ng-click="salvarEntidades()" data-dismiss="modal" accesskey="s">Salvar</button>

But how to put e+p example?

  • It is not possible to assign more than one key to the attribute accesskeys See this article

1 answer

0


I couldn’t find a healthy way to do it with just the attribute accesskey, but with javascript/jQuery besides being relatively simple, you can prevent default browser actions, as in the example explained below:

//Crie um map para as keys que você vai querer utilizar
var map = {65: false, 81: false,17:false};
//Usando o evento keydown partindo do documento
$(document).keydown(function (e) {
  //Prevenindo o default
  e.preventDefault();
  //Caso a tecla pressionada tenha sido definida no mapa acima entra  no contexto
  if (e.keyCode in map) {
      //Muda o key pressionado para true no mapa
      map[e.keyCode] = true;
      //Verificando os keys
      if (map[65] && map[81]) {
          //Faça algo ao pressionar Q + A
          console.log('Pressionado Q + A');
      }
      if (map[17] && map[65]) {
          //Faça algo ao pressionar CTRL + A
          console.log('Pressionado CTRL + A');
      }
  }
}).keyup(function (e) {
  //Aqui limpando e devolvendo o valor false para o mapa pra saber que foi solto o botão
  if (e.keyCode in map) {
      map[e.keyCode] = false;
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Browser other questions tagged

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