2

I want to disable F8 key on my web page. Is there any way I can disable it using jquery or any associated plugins or just javascript??

Thanks in advance... :)

blasteralfred

Alfred
  • 21,058
  • 61
  • 167
  • 249

3 Answers3

2

Like this Disable F5 key in Safari 4

but using keyCode 119:

<script>
var fn = function (e)
{

    if (!e)
        var e = window.event;

    var keycode = e.keyCode;
    if (e.which)
        keycode = e.which;

    var src = e.srcElement;
    if (e.target)
        src = e.target;    

    // 119 = F8
    if (119 == keycode)
    {
    alert('nope')
        // Firefox and other non IE browsers
        if (e.preventDefault)
        {
            e.preventDefault();
            e.stopPropagation();
        }
        // Internet Explorer
        else if (e.keyCode)
        {
            e.keyCode = 0;
            e.returnValue = false;
            e.cancelBubble = true;
        }

        return false;
    }
}
document.onkeypress=document.onkeydown=document.onkeyup=fn
</script>
Community
  • 1
  • 1
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • it worls when i click page.. but not working when i click address bar or i click inside pdf.. – Alfred Apr 04 '11 at 15:47
  • That is correct. PDF will not respond to any javascript changes in the page. A PDF is like a black hole, only accessible by Acrobat or whatever reader you have installed – mplungjan Apr 04 '11 at 15:54
  • yea i got it `:)` .... any way thanks for your support.. let me dig deep into it.. Do you know any browser supported reader other than adobe reader which prevent pdf saving?? – Alfred Apr 04 '11 at 16:01
  • You can disable saving in the PDF itself. – mplungjan Apr 04 '11 at 16:03
  • 1
    If you're trying to stop people from copying your PDF there is no way that Javascript will help you. The PDF is already downloaded and saved on the user's machine (albeit in a temp file) when they view it. Any semi-power user can just open up his temp files, locate the last downloaded PDF, rename it and send it to all his friends. – James Apr 04 '11 at 21:03
1

Have you tried something like this?

$(document).keydown(function(e){
    if(e.which === 119){
        return false;
    }
});

i created a jsfiddle sandbox where you can test it (works):

http://jsfiddle.net/alzclarke/yW6H3/

alzclarke
  • 1,725
  • 2
  • 17
  • 20
1

The following code works on most browser whereas I haven't found any incompatible one yet. Let me know if it doesn't work.

The key is to re-map target event to any other original event of trivial key, i.e., make that Fn key behave as normal key.

$(document).bind("keydown", function (evt){ 
    var keycode = (evt.keyCode?evt.keyCode:evt.charCode);
    //alert(keycode);
    switch(keycode){
        case 119: //F8 key on Windows and most browsers
        case 63243:  //F8 key on Mac Safari
            evt.preventDefault();                                 
            //Remapping event
            evt.originalEvent.keyCode = 0;
            return false;
            break;
    }
});

Reference on key code and explanation on cross browser issue can be found here: quirksmode

Ken Pega
  • 534
  • 6
  • 9