Solved

Keyboard Interface to Esc Key in the SDK

Posted on 1998-11-07
3
506 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

770 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