AccessKey in Jquery

1

On a page I have several buttons with their respective accesskey (shortcut key for it), how do I do when this button is called by the key to execute the click action of it in Jquery. Example: I have the Send button, with a shortcut to the A key, when I press the A key (accesskey of it) I call the button click function in Jquery.

    
asked by anonymous 05.02.2016 / 04:23

1 answer

1

I'm not exactly sure how you're adding these event buzzers. If you add this in the question it becomes clearer your implementation.

Some examples of how you can do this:

If you are adding locally

If what you use is something like: $('.button').on('click, keyup', function(){ then you can add an event type check 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 this implies that it is a key event.

If you have a global handset

If you have something like $(document).on('keyup', function(){ that will check the 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 the page load:

var btnsTeclas = {};
$('button[data-tecla]').each(function(){
    var tecla = $(this).data('tecla');
    btnsTeclas[tecla] = this;
});

And when you know the key you just have to do it:

var teclaPressionada = event.key; // ou outra lógica que tenhas para saber a tecla pressionada
$(btnsTeclas[teclaPressionada]).click();
    
05.02.2016 / 07:13