I don’t know exactly how you are adding these event headphones. Adding this to the question makes your implementation clearer.
Some examples of how you can do this:
If you’re adding locally
if what you wear is something like: $('.button').on('click, keyup', function(){
then you can add a check of the event type like this:
$('.button').on('click, keyup', function(e){
if (e.key == 13){
// correr a lógica do código para a tecla Enter, e depois
$(this).click();
}
// etc
or alternatively if (e.type != 'click') $(this).click();
, but if you already have a logic to detect the key it implies that it is a keystroke event.
If you have a global handset
If you’ve got something like that $(document).on('keyup', function(){
which will check which key then you should have an object that stores the relationship between buttons and keys... for example:
Button 1
and then you can cache it at the beginning of page loading:
var btnsTeclas = {};
$('button[data-tecla]').each(function(){
var tecla = $(this).data('tecla');
btnsTeclas[tecla] = this;
});
and when you know what the key is, just do it:
var teclaPressionada = event.key; // ou outra lógica que tenhas para saber a tecla pressionada
$(btnsTeclas[teclaPressionada]).click();
If you can, comment on the code you are using.
– Matheus