Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery : how to use keyboard shortcut F2 and F3

Good day,

Im wondering if we can use keyboard shortcut F2 and F3 to execute function. If got, maybe can share your code to me. Below my idea to make the shortcut key. i've tried but not functioning.

$("#ENQUIRY_VIEWMETER").keypress(function(event) {
    if(event.which == 113) { //F2
        updateMtr();
    } else if(event.which == 114) { //F3
        resetView();
    }
});

p/s : or maybe need some amendment on my code. :)

like image 552
hemiz Avatar asked Dec 13 '22 00:12

hemiz


1 Answers

Try using the keydown event instead of keypress. The keydown event tells you which actual key was pressed, but keypress is more about what character resulted.

And return false so that the default browser behaviour (if any) for those keys doesn't go ahead (don't return false for other keys).

$("#ENQUIRY_VIEWMETER").keydown(function(event) {
    if(event.which == 113) { //F2
        updateMtr();
        return false;
    }
    else if(event.which == 114) { //F3
        resetView();
        return false;
    }
});

Demo: http://jsfiddle.net/TTrPp/

like image 86
nnnnnn Avatar answered Feb 28 '23 01:02

nnnnnn