capturing f5 keypress event in javascript using window.event.keyCode in [removed] event is always 0 and not 116

后端 未结 7 894
南方客
南方客 2020-11-29 09:02

i am creating an MVC application.There was a neccessitity to make a variable in a session to null upon closing of the application (i.e. window/tab) but not upon refreshing t

7条回答
  •  孤独总比滥情好
    2020-11-29 09:27

    Modified version and working also after pressing 't'.

    key 116 getting pressed auto after 84

    document.onkeydown = fkey;
    document.onkeypress = fkey
    document.onkeyup = fkey;
    
    var wasPressed = false;
    
    function fkey(e){
        e = e || window.event;
       if( wasPressed ) return; 
    
        if (e.keyCode == 116) {
             alert("f5 pressed");
    
        }else {
            alert("Window closed");
        }
        wasPressed = true;
    }
    

提交回复
热议问题