We help IT Professionals succeed at work.

Accelerators for another window

holz
holz asked
on
Medium Priority
714 Views
Last Modified: 2013-11-20
Hi,

I've got an MFC app with MDI interface, keyboard accelerators an some (small) floating windows.

The problem is: Whenever a floating (=modeless) window is active (and thus the MDI document inactive) the keyboard accelerators won't work.

I'd like to change that, but don't know how. Moreover, keyboard shortcuts from the floating window should still work. Only when the user presses a key that the floating window can't handle, the document window should be informed (probaly by receiving a WM_COMMAND).

The floating window has the HWND of the current topmost document windows. But how do I catch and relay the message?

Thanks,

Thomas
Comment
Watch Question

Commented:
Override your floating window OnCommand fnc to pass the accelerator messages that the floating window does not treat to the view via SendMessage or PostMessage.
Here's I got around the problem of accelerators not working:
// Load the accelerator table, say into your main app
HACCEL hAccel = ::LoadAccelerators(NULL,MAKEINTRESOURCE(IDR_MAINFRAME));

At particular spots in your modeless dialog, call the function below the the hWnd being your main window and hAccel being from above:

void ClearTheMsgQueue(HWND hWnd,HACCEL hAccel)
{
  MSG Message;
  while ( ( ::PeekMessage( &Message, NULL, 0, 0, PM_REMOVE ) )
  {
    int nSuccess = TranslateAccelerator(hWnd,hAccel,&Message);
    if (nSuccess == 0)
    {
      ::TranslateMessage( &Message );
      ::DispatchMessage( &Message );
    }
  }
}

   Glenn
void ClearMsgQueueTrans()
{
  CCycloneApp * pApp =  (CCycloneApp *)AfxGetApp();
  ClearMsgQueue(pApp->m_pMainWnd->m_hWnd,pApp->m_hAccel);
}
Oops! Ignore the code below my name

Author

Commented:
Hello Glen,

thanks, that worked (and was easier than expected). ;)

Just one comment: I didn't use "particular spots", but just overrode the PreTranslateMessage function of the window and inserted the TranslateAccelerator call.

Greetings,

Thomas
Glad it worked!  My code snippet was required because we had message handlers that would execute for hours (no kidding).  So, at strategic spots, we called the function ClearTheMsgQueue.  But, without the TranslateAccelerator call we noticed when the user clicked "Alt+S" nothing happened.  Here's the weird thing - if no message handler was executing when they clicked "Alt+S" then the code for "Alt+S" did execute.  
  Anyway, you might want to test your code under the two situations I described above.  Namely, does the accelerator work when a message handler is currently executing and when there isn't a message handler executing.
   
   Glenn

Explore More ContentExplore courses, solutions, and other research materials related to this topic.