Page 1 of 1

VGUI menu that captures the ESC key

PostPosted: Mon Jul 29, 2013 2:47 pm
by SM Sith Lord
I have some VGUI panels that receive keyboard input, but I can't figure out how to capture the ESC key. They inherit from vgui::Frame. The problem is when you press ESC, it hides the panel without executing my close code, which causes a crash on HL2 exit and some other problems for me.

Is there a way to capture the ESC button to change the default behavior from hiding the panel to calling the OnCommand("close") method?

Re: VGUI menu that captures the ESC key

PostPosted: Mon Jul 29, 2013 4:20 pm
by ScarT
Try

Code: Select all
void <YourPanel>::OnKeyCodeTyped( vgui::KeyCode code )
{
   if ( code == KEY_ESCAPE   )
   {
      // Do nothing or tell the user it isn't possible?
      Msg("BEEP BOOP DISABLED\n");
   }
   else
   {
      // Make sure everything else gets sent properly
      BaseClass::OnKeyCodeTyped( code );
   }
}

Re: VGUI menu that captures the ESC key

PostPosted: Wed Jul 31, 2013 6:09 pm
by zombie@computer
The Esc key is swallowed up before the keyevent even fires iirc.

Re: VGUI menu that captures the ESC key

PostPosted: Fri Aug 02, 2013 2:13 am
by SM Sith Lord
That is correct zombie@comp. Pressing escape does not make the OnKeyCodePressed method fire.

Re: VGUI menu that captures the ESC key

PostPosted: Fri Aug 02, 2013 2:46 am
by SM Sith Lord
If you override the OnKeyTyped message of Frame instead, you can monitor the escape key being pressed but doesn't look like you can stop its default behavior.

Code: Select all
void CPopUpMenu::OnKeyTyped(wchar_t unichar)
{
   if( unichar+0 == 27 )
      this->OnCommand("Close");
}

Re: VGUI menu that captures the ESC key

PostPosted: Sat Aug 03, 2013 12:33 pm
by Sandern
SM Sith Lord wrote:If you override the OnKeyTyped message of Frame instead, you can monitor the escape key being pressed but doesn't look like you can stop its default behavior.

Code: Select all
void CPopUpMenu::OnKeyTyped(wchar_t unichar)
{
   if( unichar+0 == 27 )
      this->OnCommand("Close");
}

Inside ClientModeShared::KeyInput (or your derived mod implementation) you need to check for KEY_ESCAPE and return 0. That will suppress the default behavior.

Re: VGUI menu that captures the ESC key

PostPosted: Tue Aug 06, 2013 3:28 pm
by SM Sith Lord
Thanks for the lead. That ClientModeShared::KeyInput gets called for every button I press EXCEPT for the escape key, so still no luck.

Hooking into the OnKeyTyped method like in my code example only seems to work for a specific panel too, which is strange. Seems like the engine is guarding the escape key with its life :(

Re: VGUI menu that captures the ESC key

PostPosted: Tue Aug 06, 2013 6:28 pm
by SM Sith Lord
Ok, so the seemly randomness of the OnKeyTyped method being called is because when you click on a textbox or anything else in the panel, that specific object captures the OnKeyTyped msg instead.

Anyways, what I really needed to do to capture the escape key being pressed was add this to my panel:
Code: Select all
   MESSAGE_FUNC( Close, "Close" );
   MESSAGE_FUNC( CloseModal, "CloseModal" );


When ESCAPE is pressed, the CloseModal (and I assume Close for non-modal frames) gets called on your panel. Do your clean up in there.

Re: VGUI menu that captures the ESC key

PostPosted: Wed Dec 04, 2013 7:09 pm
by SM Sith Lord
To prevent the engine from automatically handling the ESCAPE key, you need this to happen:

Code: Select all
bool CHLClient::HandleUiToggle() { return true; }