[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

WM_KEYDOWN shortcuts for Menus in a split view

Posted on 2008-06-23
7
Medium Priority
?
862 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

 
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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

649 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