0

I need a JavaScript script that would disable users from closing using ALT+F4. I have looked everywhere but it just everyone just says it isn't advised.

May not even be possible, if it isn't I will just have to detect when the user does quit out this way and log it in a database.

Here is my current script, it detects if the user presses either ALT or F4, but I can't get it to cancel that key press. Is there a way to make the browser think the user pressed another key as well so the combo would be ALT + G + F4 for example, which would disrupt the ALT+F4 combo?

//Run on keydown, disable user from quiting via ALT+F4
document.onkeydown = function(evt) {
    evt = evt || window.event;
    //Get key unicode
    var unicode = evt.keyCode ? evt.keyCode : evt.charCode;

    //Check it it's ALT or F4 (115)
    if (unicode == 115 || evt.altKey == 1)
    {
        window.event.cancelBubble = true;
        window.event.returnValue = false;
    }
};
Martyn Ball
  • 4,679
  • 8
  • 56
  • 126
  • 5
    Not possible. JavaScript, which is sandboxed within the browser, can't prevent a user from interacting with his window manager or operating system. (And I'm very thankful for that.) (Also, Alt-F4 isn't the only way to close something and only applies to Windows environments anyway.) – David Nov 12 '13 at 19:54
  • @David -- Not yet... *evil thoughts* – tymeJV Nov 12 '13 at 19:54
  • You're better off doing your sign-out script JavaScript on the [`window.unload()`](https://developer.mozilla.org/en-US/docs/Web/API/Window.onunload) event, which triggers when the user closes the window & not preventing the default mechanism - Or, keep Session in Cookies to match when the person re-logs in, if no match. Log out. – MackieeE Nov 12 '13 at 19:54
  • 1
    Do you need to do that only for `Alt+F4` or for any of the 1001 ways to close a window? – Vatev Nov 12 '13 at 19:55
  • 1
    Are you also going to detect when the power goes out, the network connection goes down, Or the computer crashes? If not, you should think of a better way to detect if the user disconnects. – some Nov 12 '13 at 19:58

3 Answers3

10

That key event is (on most OSs I guess) processed by the OS before it'S even sent to the browser, so cancelling the event inside the browser won't help a thing - even if it was Javascript that is executed inside the browser's UI, not only the current document.

Therefore - what you're trying to do cannot be done.

Kishor Prakash
  • 8,011
  • 12
  • 61
  • 92
Johannes H.
  • 5,875
  • 1
  • 20
  • 40
5

One can prevent the resulting window closure with:

window.onbeforeunload = funcRef

I think readers landing here might want to be reminded of this (as I did).

For related details, see Jquery prevent window closing

Community
  • 1
  • 1
www-0av-Com
  • 707
  • 10
  • 14
-1

try to use the code below:

window.webContents.on("before-input-event",(event,input)=>{
if(input.code=='F4'&&input.alt)event.preventDefault();});
Mahyar Mottaghi Zadeh
  • 1,178
  • 6
  • 18
  • 31
Om..
  • 11
  • 2