[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

WM_KEYDOWN shortcuts for Menus in a split view

Posted on 2008-06-23
7
Medium Priority
?
863 Views
Last Modified: 2013-12-14
I need to use keyboard shortcuts in my MFC app, which is a form based application using CDocumentTemplate like this:

            pDocTemplate = new CSingleDocTemplate(
                  IDR_MAINFRAME,
                  RUNTIME_CLASS(CMyProgDoc),
                  RUNTIME_CLASS(CMainFrame),       // main SDI frame window
                  RUNTIME_CLASS(CMyProgView));

I would like to know how I should process keystrokes in PreTranslateMessage so that they process menu commands.  

Currently all my menu  functions are located in MyProgView.  But keystrokes are being caught in MyProg.

Should I use messages, or is there some other way to access MyProgView from MyProg directly?

I'm trapping KeyDown's in MyProg.cpp like this:

BOOL CMyProgApp::PreTranslateMessage(MSG* pMsg)
{
      if ( pMsg->message == WM_KEYDOWN)
      {
            if (GetKeyState(VK_CONTROL)<0)
            {
                  if (pMsg->wParam=='C')
                  {
                        // ???? would like to make a menu event happen here
                        return TRUE;
                  }
                  if (pMsg->wParam=='K')
                  {
                        // ???? would like to make a menu event happen here
                        return TRUE;
                  }
            }
      }

I suspect this kind of question has been answered millions of times already, but I'm currently at a loss.  I'm not a C++ expert but this app really needs to use this language due to some low level requirements that most other languages can't accomplish.
0
Comment
Question by:dparkes
  • 4
  • 3
7 Comments
 
LVL 19

Expert Comment

by:mrwad99
ID: 21853807
At first glance, you are returning TRUE from PreTranslateMessage(), surely you should return FALSE to let processing continue within the main frame and then the active view?
0
 
LVL 19

Accepted Solution

by:
mrwad99 earned 2000 total points
ID: 21857440
I have just played with this and my statement above could do with some more elaboration.

MFC will handle messages in the order of App, then mainframe, then active view and active document.  If you handle your messages in the app, and return TRUE from PreTranslateMessage, you effectively cut short this circuit.  By returning FALSE, you state that the message has not been handled.  However, in this case, we should return the base class implementation, i.e. CWinApp::PreTranslateMessage().  This will forward the message onto the main frame, then the active view etc etc.

The first thing to do is remove your code for testing for keystrokes, or better yet, remove the declaration and definition of CMyProgApp::PreTranslateMessage.  That way, the inherited version will be called, which will do the forwarding mentioned above.

Moving on...

I take it you are using Visual Studio to handle these messages.  If you are then you need to add a handler for the menu items (right click each menu item ->Add Event handler, choose the class to handle the message in, say, your MyProgView class), then AppWizard will add a handler function to the class along with the required message map entry so the message routing process can find the handler in your view.  The message map entry and function added might look like this one I added for a File|Test menu entry:

ON_COMMAND(ID_FILE_TEST, &MyProgView::OnFileTest)

void MyProgView::OnFileTest()
{
      // Handle menu being clicked here
}

Now, regarding handling accelerators, i.e. Ctrl + <Keystroke>, you need to add an accelerator table to your resources, which you should already have if you have created the app using AppWizard.  In the case above, I added an accelerator for ID_FILE_TEST, with the modifier of Ctrl, the key of 'T' and the type of 'VIRTKEY'.

This really is all there is to it :o)

HTH
0
 
LVL 1

Author Closing Comment

by:dparkes
ID: 31471244
Thanks for that detailed explanation.  Sorry for the delay getting back, but I only can work on my project in my spare time.  I'll give your suggestions a try.  I'm sure they'll work!
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 19

Expert Comment

by:mrwad99
ID: 21881847
No problem.  Any problems post back here.
0
 
LVL 1

Author Comment

by:dparkes
ID: 21890946
Actually, this was my understanding of how it should work.  But, for some unknown reason, I can only get pretranslatemessage to work in the App.  I tried placing it only in the View and it is not being called.  Neither is keydown or the accelerators in the resource file.  I guess there is something blocking it but I can't find it.  BTW, the menu events are working fine, so I can enable/disable and trigger menu selections.
0
 
LVL 1

Author Comment

by:dparkes
ID: 21891021
I just realized, Pretranslatemessage is being called in the View, but WM_KEYDOWN is not being detected.  Instead the computer beeps whenever a key is pressed.
0
 
LVL 1

Author Comment

by:dparkes
ID: 21892974
Okay, I think I figured it out.  Just as an FYI, here is what I discovered about my app.  Without giving it much thought, I was using multiple dialogs to switch from one view to another, to hide and show views depending on what the user selects, and these dialogs were the source of the beep when a key was pressed.  The correct dialog was not in focus at the time, so PreTranslateMessage appears to work as long as I use the Setfocus command after a particular view/dialog is selected.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

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

How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

834 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