Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firing ctrl+r, ctrl+a, ctrl+q events on a button click

I need to fire the Ctrl+R, Ctrl+A, Ctrl+Q events when a user clicks on a button.

I was working on the following code:

$(document).ready(function () {
    $('#Button1').click(function () {
        var evt = $.Event("keypress");
        evt.keyCode = 81;
        evt.ctrlKey = true;
        evt.shiftKey = true;
        $(document).trigger(evt);
    });
});
like image 409
Jeevash Avatar asked Nov 01 '22 17:11

Jeevash


1 Answers

You can't simulate browser control keys, but you can simulate their effects.

Ctrl-R refreshes.

function refresh() {
    location.reload(true);
} 

Ctrl-A selects everything. Code is from here.

function selectAll() {
    var e = document.getElementsByTagName('body')[0];
    var r = document.createRange(); r.selectNodeContents(e);
    var s = window.getSelection();
    s.removeAllRanges();
    s.addRange(r);
}

I am not sure what Ctrl-Q is supposed to do; if it quits the browser, that one isn't possible.

like image 162
PurkkaKoodari Avatar answered Nov 12 '22 12:11

PurkkaKoodari