hemiz
hemiz

Reputation: 91

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. :)

Upvotes: 5

Views: 14098

Answers (3)

udai veer
udai veer

Reputation: 1

document.onkeyup = KeyCheck;

function KeyCheck(e){

   var KeyID = (window.event) ? event.keyCode : e.keyCode;

   if(KeyID == 113){ 

      updateMtr();

   } else if(KeyID == 114) {

       resetView();
    }
}

Upvotes: 0

nnnnnn
nnnnnn

Reputation: 150080

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/

Upvotes: 12

Nikson Kanti Paul
Nikson Kanti Paul

Reputation: 3440

try like this

$(document).keypress(function(e){
   if(e.charCode == 97){
      alert("a");
      return false;
  }
 })

check this jQuery HotKey plugin

Upvotes: 2

Related Questions