Cmd-Q interceping

Hi all,

How can i catch user pressing Cmd-Q, and hook it up to the event, instead of instant quiting? It seems OnApplicationQuit() and Application.CancelQuit() is not helping here.

Alexey

I’m not sure why, because that’s what I use and it works fine.

–Eric

So, what’s wrong with this:

bool m_cmd_q = false;

void OnApplicationQuit()
	{
		if( !m_cmd_q )
		{
			if( (Input.GetKeyDown("right cmd") || Input.GetKeyDown("left cmd"))  Input.GetKeyDown("q") )
				m_cmd_q = true;
		}

		if( m_cmd_q )
		{
			Application.CancelQuit();
			ShowMenuDo();
		}
	}

It doesn’t work, always quits instanly.

GetKeyDown only returns true for the frame that the key is actually pressed down. However, I’m not sure why you’re checking for that at all, particularly since that wouldn’t cover quitting from the menu (or Windows builds)…I would think you’d just do

void OnApplicationQuit()
	{
		Application.CancelQuit();
		ShowMenuDo();
	}

–Eric

But how should i quit application in that case? I just want to display “Quit? Yes/No” menu on Esc and Cmd-Q, but if player pressed “Yes”, i need actually quit (use Application.Quit()). But wouldn’t it invoke OnApplicationQuit() again?

Yes, but see the example in the docs under CancelQuit for how to handle that.

–Eric

Thanks! I understand it now.