jquery : how to use keyboard shortcut F2 and F3
12,669
Solution 1
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/
Solution 2
try like this
$(document).keypress(function(e){
if(e.charCode == 97){
alert("a");
return false;
}
})
check this jQuery HotKey plugin
Author by
hemiz
Updated on June 17, 2022Comments
-
hemiz about 2 years
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. :)
-
Nikson Kanti Paul almost 12 yearssearch before post keyboard-shortcuts-with-jquery
-
-
hemiz almost 12 yearsGot it!!.. i knew it, its not that hard.. thx for the answer!!
-
nnnnnn almost 12 yearsOP wants to know how to test for function keys.
keypress
is the wrong way to do that (otherwise the code in the question would work). -
Admin over 2 yearsYour answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
-
nima over 2 yearsWhile this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. You can find more information on how to write good answers in the help center: stackoverflow.com/help/how-to-answer . Good luck 🙂