Solved

WM_KEYDOWN shortcuts for Menus in a split view

Posted on 2008-06-23
7
856 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Use of condition with 'serial' in ansible 2 62
wordappend challenge 8 181
passing data from one form to another form in c++ 27 80
C++ :Change value from  DisableCMD registry 4 60
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
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.

832 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