VGUI menu that captures the ESC key

Grab your favourite IDE and tinker with the innards of game engines

VGUI menu that captures the ESC key

Postby SM Sith Lord on Mon Jul 29, 2013 2:47 pm

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?
SM Sith Lord
Been Here A While
Been Here A While
 
Joined: Sat Nov 25, 2006 4:25 pm
Location: Los Angles, CA

Re: VGUI menu that captures the ESC key

Postby ScarT on Mon Jul 29, 2013 4:20 pm

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 );
   }
}
User avatar
ScarT
Senior Member
Senior Member
 
Joined: Sat Apr 02, 2005 7:33 pm
Location: Denmarkian Land

Re: VGUI menu that captures the ESC key

Postby zombie@computer on Wed Jul 31, 2013 6:09 pm

The Esc key is swallowed up before the keyevent even fires iirc.
When you are up to your neck in shit, keep your head up high
User avatar
zombie@computer
Forum Goer Elite™
Forum Goer Elite™
 
Joined: Fri Dec 31, 2004 5:58 pm
Location: Lent, Netherlands

Re: VGUI menu that captures the ESC key

Postby SM Sith Lord on Fri Aug 02, 2013 2:13 am

That is correct zombie@comp. Pressing escape does not make the OnKeyCodePressed method fire.
SM Sith Lord
Been Here A While
Been Here A While
 
Joined: Sat Nov 25, 2006 4:25 pm
Location: Los Angles, CA

Re: VGUI menu that captures the ESC key

Postby SM Sith Lord on Fri Aug 02, 2013 2:46 am

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");
}
SM Sith Lord
Been Here A While
Been Here A While
 
Joined: Sat Nov 25, 2006 4:25 pm
Location: Los Angles, CA

Re: VGUI menu that captures the ESC key

Postby Sandern on Sat Aug 03, 2013 12:33 pm

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.
Sandern
Regular
Regular
 
Joined: Thu Jun 30, 2005 1:08 pm
Location: Netherlands

Re: VGUI menu that captures the ESC key

Postby SM Sith Lord on Tue Aug 06, 2013 3:28 pm

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 :(
SM Sith Lord
Been Here A While
Been Here A While
 
Joined: Sat Nov 25, 2006 4:25 pm
Location: Los Angles, CA

Re: VGUI menu that captures the ESC key

Postby SM Sith Lord on Tue Aug 06, 2013 6:28 pm

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.
SM Sith Lord
Been Here A While
Been Here A While
 
Joined: Sat Nov 25, 2006 4:25 pm
Location: Los Angles, CA

Re: VGUI menu that captures the ESC key

Postby SM Sith Lord on Wed Dec 04, 2013 7:09 pm

To prevent the engine from automatically handling the ESCAPE key, you need this to happen:

Code: Select all
bool CHLClient::HandleUiToggle() { return true; }
SM Sith Lord
Been Here A While
Been Here A While
 
Joined: Sat Nov 25, 2006 4:25 pm
Location: Los Angles, CA

Return to Programming

Who is online

Users browsing this forum: No registered users