Solved

WM_KEYDOWN shortcuts for Menus in a split view

Posted on 2008-06-23
7
855 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 500 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
modThree challenge 4 79
zeroFront challenge 7 80
Change local server setting in php 6 82
Beginner to Unreal Engine 4 5 61
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…
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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

895 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now