Solved

WM_KEYDOWN shortcuts for Menus in a split view

Posted on 2008-06-23
7
854 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 19

Expert Comment

by:mrwad99
Comment Utility
No problem.  Any problems post back here.
0
 
LVL 1

Author Comment

by:dparkes
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.

771 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

12 Experts available now in Live!

Get 1:1 Help Now