4

I'm trying to use the function keys in a web application. For example, if the user presses F2 or F3, the app must do something. Is this possible? Thanks

mtmacdonald
  • 14,216
  • 19
  • 63
  • 99
stighy
  • 7,260
  • 25
  • 97
  • 157
  • Check this. http://stackoverflow.com/questions/424407/handling-key-press-events-f1-f12-using-javascript-and-jquery-cross-browser – specialscope Oct 19 '12 at 07:47

1 Answers1

3

Here's a jQuery solution, using keydown.

<!doctype html>
<html>
 <head>
 <script type="text/javascript" src="jquery.min.js"></script>
 <script type="text/javascript">
    $(document).ready(function(){
        $("body").keydown(function(key) {
            if (key.which == 113) { // F2 key
                alert("testing");
            }
        });
    });
 </script>
 </head>
 <body>
  Test page. 
 </body>
</html>

Note - the function keys might be reserved for other uses (e.g. F5 is 'refresh page'), so using these keys might not be advisable.

mtmacdonald
  • 14,216
  • 19
  • 63
  • 99