r/howdidtheycodeit • u/Natalwho • Sep 27 '24
How did they code Alt f4 bypassing in gta v?
Like the title says, gta 5 is the only game- or process for that matter, that ive ever used which isnt killed by alt f4. How did they do that? Do they write a rule or something within windows itself, like in the registry? Id like to create a system that quickly saves the game when the player hits alt f4, before ending the process, for qol.
54
Upvotes
234
u/CowBoyDanIndie Sep 27 '24
Alt f4 sends a close message to the program, the program can just ignore it, or save and then exit, it’s up to the program.
30
u/Henrarzz Sep 27 '24
Both Alt+F4 pressing event and window close event can be intercepted by programmer in standard Windows message pump
23
u/KiwasiGames Sep 27 '24
Any process can ignore Alt F4. Alt F4 results in the operating system telling process “the user would like you to quit now please”. Traditionally processes have used this as a signal to “drop everything and quit”. But there is nothing mandating a process to do this. The process can instead say “thanks, I’m going to save some stuff and clean up my room before I go”. Or the process can simply say “no thanks”. This last one is so that the process can do things like display an “are you sure” dialogue and cancel the quit command if the user chooses to.
From memory there are some cases where the OS will enforce Alt F4. For example if the process refuses to acknowledge the request at all and the user keeps pressing Alt F4. But it’s generally not desirable to force a process to end from the outside.
If you really want to kill a process dead right now with no questions asked, ctrl alt del is your friend. In windows at least, this cannot be bypassed or intercepted by the process.
42
u/sauterj Sep 27 '24
It depends on the engine probably, but one that I'm familiar with, and have done this exact thing for, is in Unity. Unity has a concept of "wantsToQuit" (which would be triggered by the player hitting Alt+F4) that you can subscribe to and optionally cancel the quit request by returning false. Here's a link to what that code looks like in my in-development game.
Granted, Rockstar uses a different engine (RAGE) for GTA, so their solution may look different, but I hope at least seeing an example can help you understand how it works!
3
u/Poddster Sep 27 '24
Really? The browser I'm using right now responds to alt-f4. Practically every non-game process responds to alt-f4! Even most games do, as I used it the other day in one. alt+f4 is simply a hotkey for pressing the X on a window. You can see this by clicking on the application icon in a window's titlebar, or pressing
alt+space, to bring up the "system menu" drop down menu. It clearly listsalt+f4as the hotkey for thecloseaction.https://superuser.com/a/1482238/4216
As to the answer of "how they coded it": The first chapter of the initial win32 tutorial teaches you about the windows message pump and about
WM_CLOSE(akaalt+f4). Most games respond toWM_CLOSEbecause it's what happens when you press the X on a window, so it's pretty baffling that GTA5 is the only process you've found this working on.https://learn.microsoft.com/en-us/windows/win32/learnwin32/closing-the-window
4
u/darkname324 Sep 27 '24
source2 engine games ignore alt f4 aswell
2
u/fuzzynyanko Sep 27 '24
You can absolutely do a save before quitting normally. Don't take too long and you should be good. I would do the standard "save to temp and rename" just in case. This is standard Windows messaging flow though. Different game engines might make this tricky
If the saving takes a while, the application can look frozen, even if it's actually processing.
2
u/wizard_mitch Sep 27 '24
Since they use their own engine that is just the choice they make. Most low level frameworks are going to let the programmer decide how to handle and received events.
Using GLFW as an example.
Typically this is stored in a flag which is evaluated in a loop or by a callback function, GLFW has both.
1
u/[deleted] Sep 27 '24 edited Sep 15 '25
Suddenly, a parade of singing pencils marched through the door, distributing philosophical questions to anyone who would listen. The walls, feeling increasingly ignored, began to paint themselves with the color of unspoken thoughts. A rogue pineapple, claiming to be the true leader of the entire event, demanded a crown made of recycled moonbeams, but the floor refused to participate, stating it was simply too tired of always being walked on.
1
u/_abscessedwound Sep 27 '24
The specific combinations of keys that kill a process can be intercepted in a lot of event-driven systems, since they sit atop the OS handler. Giving those combos a custom or dummy handler is pretty trivial at that point.
1
u/[deleted] Sep 27 '24
CS2 also ignores alt+f4 if I'm not mistaken
1
u/[deleted] Sep 29 '24
You just add a signal capture it’s a feature of all programming languages that can interface with C
1
u/PocketMartyr Sep 30 '24
Dead by Daylight also does not close with alt - f4
1
u/Gibgezr Sep 27 '24
It's ok to ignore alt-F4, but you absolutely should NOT remap it to quicksave. Teaching someone that alt-F4 saves is a very bad idea, as they might have muscle-memory kick in when playing another game and most games will unceremoniously exit on alt-F4, without saving. You never want to train someone to press alt-F4 unless they want to immediately exit.