use f2 to click button with jquery

3

I have the following button on my site:

<button onclick='document.location="google.com.br"'>FINALIZAR</button>

How do I get it to click when I press f2? I wanted to do this using jquery.

Can anyone help me?

    
asked by anonymous 29.11.2016 / 19:30

4 answers

2

Use the keyup event and check that the keycode is 113 (F2), then just invoke the event of the element you want.

$(document).on('keyup', function(e) {
  var keyCode = e.keyCode || e.which;
  if (keyCode === 113)
    $('#elemento').trigger('click');
});

$('#elemento').on('click', function () {
  console.log('Disparado evento');  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><ahref="javascript:void(0)" id="elemento" >Exemplo</a>
    
29.11.2016 / 19:37
2

You can identify the click event on the body and if f2 clicks the button.

$('body').keypress(function(e) {
  var code = e.keyCode || e.which;
  if (code == 113) { // 113 = f2
    $('#btn').click();
  }
});

function finalizar() {
  console.log("Fui clicado");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><buttonid="btn" onclick="finalizar()">FINALIZAR</a>
    
29.11.2016 / 19:37
2

You can also do this:

document.onkeyup = KeyCheck;

function KeyCheck(e)
{
    var tecla = (window.event) ? event.keyCode : e.keyCode;
    
    if (tecla == 113) {
      alert('Pressionou F2') // Aqui você coloca seu clique no botão
    }
}
    
29.11.2016 / 19:43
1

Follow this solution:

<button id="finalizar" onclick='document.location="google.com.br"'>FINALIZAR</button>

$(window).on("keyup", function(event) {
    if (event.keyCode == 113) {
        $("#finalizar").trigger("click");
    }
});
    
29.11.2016 / 19:38