Link to home
Start Free TrialLog in
Avatar of ibc_web
ibc_web

asked on

Keyboard Interface to Esc Key in the SDK

I have an SDK program where I am trying to set things up so that if the user hits the ESC the program terminates.

The program is one large parent window with several smaller child window controls.  The parent window never really has the keyboard focus.  Whenever the parent window gets the keyboard focus it gives it to one of the child windows.

The user then uses the TAB key to make his/her way around the controls in a circular pattern.

To do this I have subclassed the childwindows ( gave them their own private WndProc() for keyboard processing ).

It is from this special WndProc ( SwitchFocusProc() ) that I send the destroy message.  Below is a truncated version of my code to highlight what I am doing?  Is it possible that my SendMessage() call is out of scope?.  If it is.  How can I end my program from SwitchFocusProc()?

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{

     //< Snip >
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
     //<snip>

     switch(iMsg)
     {
     case WM_CREATE:
     //<sniP>
     // Subclass Controls To SwitchFocusProc
      for( b = 0; b < nNumFocusedControls; b++ )
      {
      fnOldProc[b] = (WNDPROC)SetWindowLong(                                          ChildWindow[b + 5].hwnd,                           GWL_WNDPROC,                                     (LONG) SwitchFocusProc);            }//end for
//<snip>

LRESULT CALLBACK SwitchFocusProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
      int i, iNextWindow;
      i = GetWindowLong( hwnd, GWL_ID);

    // If The Stop Button Has The Focus Set Vars To Give
    // It To ListBox For Difficulty
   if( i == IDC_OWNERDRAW_BUTTON_STOP )
    {
     iNextWindow = IDC_LB_LEVEL;
    }
    else
    {
      iNextWindow = i + 1;
    }

    switch(iMsg)
   {
   case WM_KEYDOWN:
     if( wParam == VK_TAB )
     {
     SetFocus( ChildWindow[iNextWindow].hwnd );
     }
     else if( wParam == VK_ESCAPE )
     {
      SendMessage(hwnd, WM_DESTROY, NULL, NULL );
     }                  
     break;            
   }//end switch
 return CallWindowProc(fnOldProc[ i - 5], hwnd, iMsg, wParam, lParam);
}
ASKER CERTIFIED SOLUTION
Avatar of chensu
chensu
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ibc_web
ibc_web

ASKER

It didn't work.  Instead of shutting down the parent window it shut down the child window that happened to have the keyboard focus at the time.

My SwitchWindowProc() handles windows messages for the childwindow controls in my parent window.  However the only messages it handles are for keyboard processing.  Thus when I hit "ESC" the keyboard foucus is always possesed by a child window.

The parent window never really has the keyboard focus ( as soon as it gets it, it passes it to a control ).  

For this reason I would like the ability to have ESC destroy the program from the child windows ( SwitchFocusProc() )

Thanks for the help.......at least I got something destroyed though *smile*.

Do you think "hwnd" in SwitchFocusProc() might be out of scope for "hwnd" in WindProc()?
Avatar of ibc_web

ASKER

See the comment above.  It didn't work.  My hunch about the scope of hwnd was right.  Once I made the hwnd of the parent window global the destroy calls worked.

The calls were made from a second wndproc(), which took in its own hwnd.

Thanks for getting back to me though