Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Keyboard Interface to Esc Key in the SDK

Posted on 1998-11-07
3
Medium Priority
?
522 Views
Last Modified: 2013-12-03
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);
}
0
Comment
Question by:ibc_web
  • 2
3 Comments
 
LVL 23

Accepted Solution

by:
chensu earned 100 total points
ID: 1415830
Use
PostMessage(hwnd, WM_CLOSE, 0, 0);
instead of
SendMessage(hwnd, WM_DESTROY, NULL, NULL);

WM_DESTROY is sent by Windows to notify you that the window is being destroyed.
0
 

Author Comment

by:ibc_web
ID: 1415831
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()?
0
 

Author Comment

by:ibc_web
ID: 1415832
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
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question