javascript - Disable specific function key using jquery - Stack Overflow

admin2025-04-20  1

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

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

Share Improve this question asked Apr 4, 2011 at 15:14 AlfredAlfred 21.4k63 gold badges174 silver badges257 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 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>

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/alzclarke/yW6H3/

The following code works on most browser whereas I haven't found any inpatible 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

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745149818a287550.html

最新回复(0)