Solved

Keyboard Interface to Esc Key in the SDK

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Access Query (QBE) Using VBA Function for Criteria 13 63
Dialogbox API leak? 18 99
Need a complete list of ASP.NET page load events 3 59
C# LINQ ForEach() question 6 55
This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

820 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