HowTo detect CMD + Option + Escape

Archives Forums/MacOS X Discussion/HowTo detect CMD + Option + Escape

Ravl(Posted 2013) [#1]
Hi,

On MAC I have to save the game before the app is Force Quit.

The publishers are testing this by pressing the CMD+OPTION+Esc key in order to bring the TaskManager and then they kill the application.

I am not able to catch this event it seems, in order to save the game when the combination pressed. I tried with:

If KeyDown(KEY_LSYS) and KeyDown(KEY_LALT) And KeyDown(KEY_Scpae)

EndIf

How are


Kryzon(Posted 2013) [#2]
It's beyond my knowledge, but you need to intercept the SIGTERM signal that your application will receive.
Take a look at BlitzMax\mod\pub.mod\freeprocess.mod.

First of all, are you sure you can't intercept when they do this with AppTerminate() or EVENT_APPTERMINATE?

A short-term measure is to rather intercept a loss of focus (EVENT_APPSUSPEND) of your program and save the game then - your program will always lose focus before someone terminates it with the Activity Monitor.
For this you can add a custom hook to the EmitEventHook ID.

Function applicationHook:Object( id:Int, data:Object, context:Object )

	Local ev:TEvent=TEvent(data)
	If Not ev Then Return data

	Select ev.id
		Case EVENT_APPSUSPEND
			SaveGame() 'Lost focus, and may be terminated in the future.

		Case EVENT_APPTERMINATE		
			AskToSaveGame()
			End		
	End Select

	Return data

End Function
Adding it with...

AddHook( EmitEventHook, applicationHook, Null )

...at the beginning of your program.


xlsior(Posted 2013) [#3]
I'd assume that the task manager would actively send an event to the app to signal that a force-quit was issued, before it actually kills the task?

Are you monitoring the eventqueue?


GfK(Posted 2013) [#4]
Do you really need to do this? Surely your game is saved automatically at regular intervals throughout the game anyway?


Ravl(Posted 2013) [#5]
Well... and here it is a big 'Well...':

@Kryzon: thanks for this answer and for the code. I never used these functions.. (AddHook).

@xlsior: no!

@Gfk:
yes, this is the main issue. It's no use for me to monitoring this event in particular. the publisher is using this method (also on windows os they choose the app from Task manager and kill it) to test what is happening after thost 60 minutes of demo.

in my game, if you play for 60 minutes and you are not saving, the game will be close by their program and the user lost all his save data (in demo).

i am already know when the user is lost the focus of the window, because i am pausing the music, but the idea it's to prevent that program to kill the user experience, so.. i decided to make an auto save every 60 seconds.

hope that will do the trick


Kryzon(Posted 2013) [#6]
Make sure to build your game as a GUI app (it's an option under Program -> Build Options -> Build GUI App in the IDE).
It should make a positive difference in how the OS communicates with your application.

Also, check these:
http://stackoverflow.com/questions/4914284/how-do-i-run-code-when-a-user-agent-process-terminates
http://stackoverflow.com/questions/12313126/detect-end-process-from-task-manager-in-c-sharp