Click here to Skip to main content
Click here to Skip to main content

Keyboard messages/accelerators handling in MFC dialog based applications

By , 1 Dec 2001
 

Introduction

There are a substantial number of Windows programmers who insist, often very vehemently, that a programmer should avoid overriding PreTranslateMessage. They have their reasons for saying so and I believe they are correct. But in this article my intention is not to contemplate on whether PreTranslateMessage is good for you or whether you should avoid it like the plague. I have found that PreTranslateMessage can come in quite handy in dialog-based applications for handling keyboard messages. In addition to using PreTranslateMessage I also show you how you can override ProcessMessageFilter for handling accelerator keys in a dialog based application.

Using PreTranslateMessage to handle dialog keystrokes

Very often you hear questions from novice programmers asking how they can trap keystrokes in a dialog based application. Presumably they tried to handle WM_KEYDOWN/WM_KEYUP unsuccessfully. The whole problem is that in a dialog based application the focus is always on one of the child controls and not on the main dialog window. So what do you need to do? You need to override PreTranslateMessage. I'll show you a simple example.

Suppose that you have a dialog based app with a lot of edit boxes on the dialog. It's basically a data entry program and thus you feel it would make it easier for the end-user if pressing the ENTER key would take the focus to the next edit box, just as if he had pressed TAB. The solution is so very easy and straightforward with PreTranslateMessage as I'll demonstrate below.

BOOL CPreTransTestDlg::PreTranslateMessage(MSG* pMsg) 
{
    if(pMsg->message==WM_KEYDOWN)
    {
        if(pMsg->wParam==VK_RETURN)
            pMsg->wParam=VK_TAB;
    }	
    return CDialog::PreTranslateMessage(pMsg);
}

All I have done is to check whether the message is a WM_KEYDOWN, and if it is so, then I check to see if the wParam is VK_RETURN. If I find it so, I change the wParam to VK_TAB and then the base class implementation is called. Easy huh?

Using ProcessMessageFilter to handle dialog-based accelerator keys

Let's say you have a menu in your dialog based app. And you have an accelerator key for some particular task. You'll soon be disappointed to find that the hotkey does not work. The problem is that the modal dialog app's message loop does not call TranslateAccelerator. I do not know why this is so. Presumable the Microsoft team decided that people shouldn't use dialog based apps to write complicated applications, with hotkeys and menus.

But as usual they have suggested a workaround too. Here's is how you go about implementing it. I'd like to state again, that even though this is a Microsoft recommended technique there will be a good majority of MFC gurus, like Joseph Newcomer for example, who would tell you that you shouldn't be doing this. But then sometimes you have to sacrifice elegance for getting things done quickly and with minimum effort.

  • Add a member variable to your CWinApp derived class.
  • HACCEL m_haccel;
  • Use the resource editor to create a new Accelerator, by default it will be named IDR_ACCELERATOR1. And add a new accelerator key that is a short cut for some menu item.
  • Put the following line in your InitInstance just before the line where the CDialog derived object is declared
  • m_haccel=LoadAccelerators(AfxGetInstanceHandle(), 
            MAKEINTRESOURCE(IDR_ACCELERATOR1));
  • Now override ProcessMessageFilter and modify the function so that it looks like :-
    BOOL CPreTransTestApp::ProcessMessageFilter(int code, LPMSG lpMsg) 
    {
        if(m_haccel)
        {
            if (::TranslateAccelerator(m_pMainWnd->m_hWnd, m_haccel, lpMsg)) 
                return(TRUE);
        }
    	
        return CWinApp::ProcessMessageFilter(code, lpMsg);
    }

All we did was to call TranslateAccelerator and if it succeeds then we don't need to call the base class ProcessMessageFilter, as the message has been handled. So we return TRUE.

Disclaimer

The author wishes to state here that the two methods mentioned above are generally used methods and the author is not in any way endorsing these methods. Users should read more on the usage of PreTranslateMessage and ProcessMessageFilter before they use it in their programs.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Nish Sivakumar
United States United States
Member
Nish is a real nice guy who has been writing code since 1990 when he first got his hands on an 8088 with 640 KB RAM. Originally from sunny Trivandrum in India, he has been living in various places over the past few years and often thinks it’s time he settled down somewhere.
 
Nish has been a Microsoft Visual C++ MVP since October, 2002 - awfully nice of Microsoft, he thinks. He maintains an MVP tips and tricks web site - www.voidnish.com where you can find a consolidated list of his articles, writings and ideas on VC++, MFC, .NET and C++/CLI. Oh, and you might want to check out his blog on C++/CLI, MFC, .NET and a lot of other stuff - blog.voidnish.com.
 
Nish loves reading Science Fiction, P G Wodehouse and Agatha Christie, and also fancies himself to be a decent writer of sorts. He has authored a romantic comedy Summer Love and Some more Cricket as well as a programming book – Extending MFC applications with the .NET Framework.
 
Nish's latest book C++/CLI in Action published by Manning Publications is now available for purchase. You can read more about the book on his blog.
 
Despite his wife's attempts to get him into cooking, his best effort so far has been a badly done omelette. Some day, he hopes to be a good cook, and to cook a tasty dinner for his wife.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 3memberSharanyaMahi1 Feb '12 - 0:54 
GeneralMy vote of 5memberRethmeier27 Apr '11 - 22:26 
GeneralMy vote of 5memberMember 774968413 Apr '11 - 11:36 
GeneralMy vote of 5memberGokulnath0073 Jan '11 - 21:09 
GeneralAmazingmembervikas02312 Nov '09 - 0:45 
Generalthanks!memberanowsober19 Jan '09 - 17:30 
GeneralAccelerator for child dialogmemberhari_honey20 Aug '08 - 3:01 
GeneralRe: Accelerator for child dialogmemberDavid Johns21 Sep '10 - 16:07 
GeneralCapture consecutive VK_ENTER messagesmembermrcdsix3 Apr '08 - 8:13 
GeneralProblem with PreTranslateMessagememberasi1103 Oct '07 - 21:28 
Questioni want to call click event of button with Ctrl + Upmembermaulesh bhojani22 Aug '07 - 5:47 
QuestionI need Accelrator table in My Dll?memberramesh_5304 May '07 - 2:14 
Questiononly work after minimized it....help me please.memberfikree15 Feb '07 - 14:55 
QuestionHow to simulate the mouse behavior holding down a button with a keymemberchen_zd24 Nov '06 - 19:56 
Question! Accelerators Active In Child DialogsmemberSynetech4 Aug '06 - 20:42 
AnswerRe: ! Accelerators Active In Child DialogsmemberSynetech7 Aug '06 - 13:46 
General! Works GreatmemberSynetech22 Jul '06 - 8:20 
QuestionHow to set project for always support hots key ?memberMax++10 Jun '06 - 23:41 
AnswerRe: How to set project for always support hots key ?memberSynetech22 Jul '06 - 6:34 
Generalerror C2039: 'ProcessMessageFilter' : is not a member of 'CWinApp'memberatrung5 Jun '06 - 17:53 
General1000 thx!memberchris mook11 Apr '06 - 3:02 
QuestionHow to do this to a dialog under a child window?memberkezhu31 Mar '06 - 11:34 
QuestionHow to call a current Function now?memberbreak;11 Jan '06 - 2:00 
AnswerRe: How to call a current Function now?memberbreak;11 Jan '06 - 3:57 
GeneralACCELERATOTsusssadegh salehi12 Sep '05 - 22:34 
GeneralThanksmemberfastfootskater29 May '05 - 23:36 
GeneralAwesome job -- one correction however...memberDouglas R. Keesler30 Apr '05 - 16:46 
GeneralRe: Awesome job -- one correction however...membersmesser30 Apr '05 - 17:21 
GeneralRe: Awesome job -- one correction however...memberchakri1623 Nov '05 - 17:55 
GeneralRe: Awesome job -- one correction however...memberchakri1623 Nov '05 - 17:57 
QuestionWhat if you have an SDI with an dialog inside...memberPandele Florin28 Apr '05 - 5:04 
Generalshort but great!!!!5 for thiz!!!!!memberrateep11 Apr '05 - 1:50 
GeneralThank you!memberDavide Calabro24 Jan '05 - 4:09 
Generalmaking use of just PreTranslateMessagememberPenguen Efendi14 Nov '04 - 9:33 
GeneralPreferred solution for WinCEmemberCoruscant25 Apr '05 - 2:12 
GeneralRe: making use of just PreTranslateMessagememberMohamed Abdel-Monem23 Sep '05 - 5:03 
GeneralEnter key capturememberAchilles28 Sep '04 - 15:37 
Generalaccelerator for child dialogmemberanukutty11 Apr '04 - 18:27 
GeneralRe: accelerator for child dialogmemberchakri1623 Nov '05 - 17:59 
GeneralRe: accelerator for child dialogmemberbreak;11 Jan '06 - 3:13 
GeneralRe: accelerator for child dialogmemberbreak;11 Jan '06 - 3:57 
GeneralRe: accelerator for child dialogmemberchakri1623 Nov '05 - 19:40 
GeneralRe: accelerator for child dialogmemberchakri1623 Nov '05 - 21:37 
Generalthank umemberdivya pg11 Apr '04 - 10:12 
QuestionHow to make keyboard accelerators always visible?membernguyen_a5 Apr '04 - 8:12 
AnswerRe: How to make keyboard accelerators always visible?memberPandele Florin28 Apr '05 - 2:41 
QuestionHow to control msg in a multi-dll app?memberchifon10 Mar '04 - 1:22 
Generaldoes not work with CCtrlViewmemberpradeep gurav13 Feb '04 - 6:12 
GeneralJust Can't Get EnoughmemberAurimas Amanavičius19 Jan '04 - 8:01 
GeneralNeed Help in VC++ (Very Urgent)membereshban28425 Nov '03 - 19:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 2 Dec 2001
Article Copyright 2001 by Nish Sivakumar
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid