Click here to Skip to main content
15,126,209 members
Articles / Desktop Programming / MFC
Posted 1 Dec 2001


101 bookmarked

Keyboard messages/accelerators handling in MFC dialog based applications

Rate me:
Please Sign up or sign in to vote.
4.76/5 (81 votes)
1 Dec 2001CPOL3 min read
This article explains how you can override PreTranslateMessage and ProcessMessageFilter in dialog based apps


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) 
    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(), 
  • Now override ProcessMessageFilter and modify the function so that it looks like :-
    BOOL CPreTransTestApp::ProcessMessageFilter(int code, LPMSG lpMsg) 
            if (::TranslateAccelerator(m_pMainWnd->m_hWnd, m_haccel, lpMsg)) 
        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.


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.


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


About the Author

Nish Nishant
United States United States
Nish Nishant is a Principal Software Architect based out of Columbus, Ohio. He has over 17 years of software industry experience in various roles including Lead Software Architect, Principal Software Engineer, and Product Manager. Nish was a Microsoft Visual C++ MVP between 2002 and 2015.

Nish is an industry acknowledged expert in the Microsoft technology stack. He authored C++/CLI in Action for Manning Publications in 2005, and had previously co-authored Extending MFC Applications with the .NET Framework for Addison Wesley in 2003. In addition, he has over 140 published technology articles on and another 250+ blog articles on his WordPress blog. Nish is vastly experienced in team management, mentoring teams, and directing all stages of software development.

Contact Nish : If you are interested in hiring Nish as a consultant, you can reach him via his google email id voidnish.

Company Website :

Comments and Discussions

QuestionRestrict LeftWindowKey(VK_LWIN+D) operation Pin
chetan7866-Apr-15 3:23
Memberchetan7866-Apr-15 3:23 
GeneralMy vote of 3 Pin
SharanyaMahi1-Feb-12 1:54
MemberSharanyaMahi1-Feb-12 1:54 
GeneralMy vote of 5 Pin
Rethmeier27-Apr-11 23:26
MemberRethmeier27-Apr-11 23:26 
GeneralMy vote of 5 Pin
Member 774968413-Apr-11 12:36
MemberMember 774968413-Apr-11 12:36 
GeneralMy vote of 5 Pin
Gokulnath0073-Jan-11 22:09
MemberGokulnath0073-Jan-11 22:09 
excellent.. wat to say
GeneralAmazing Pin
vikas02312-Nov-09 1:45
Membervikas02312-Nov-09 1:45 
Generalthanks! Pin
anowsober19-Jan-09 18:30
Memberanowsober19-Jan-09 18:30 
GeneralAccelerator for child dialog Pin
hari_honey20-Aug-08 4:01
Memberhari_honey20-Aug-08 4:01 
GeneralRe: Accelerator for child dialog Pin
David Johns21-Sep-10 17:07
MemberDavid Johns21-Sep-10 17:07 
GeneralCapture consecutive VK_ENTER messages Pin
mrcdsix3-Apr-08 9:13
Membermrcdsix3-Apr-08 9:13 
GeneralProblem with PreTranslateMessage Pin
asi1103-Oct-07 22:28
Memberasi1103-Oct-07 22:28 
Questioni want to call click event of button with Ctrl + Up Pin
maulesh bhojani22-Aug-07 6:47
Membermaulesh bhojani22-Aug-07 6:47 
QuestionI need Accelrator table in My Dll? Pin
ramesh_5304-May-07 3:14
Memberramesh_5304-May-07 3:14 
Questiononly work after minimized me please. Pin
fikree15-Feb-07 15:55
Memberfikree15-Feb-07 15:55 
QuestionHow to simulate the mouse behavior holding down a button with a key Pin
chen.zd24-Nov-06 20:56
Memberchen.zd24-Nov-06 20:56 
Question! Accelerators Active In Child Dialogs Pin
Synetech4-Aug-06 21:42
MemberSynetech4-Aug-06 21:42 
AnswerRe: ! Accelerators Active In Child Dialogs Pin
Synetech7-Aug-06 14:46
MemberSynetech7-Aug-06 14:46 
General! Works Great Pin
Synetech22-Jul-06 9:20
MemberSynetech22-Jul-06 9:20 
QuestionHow to set project for always support hots key ? Pin
Max++11-Jun-06 0:41
MemberMax++11-Jun-06 0:41 
AnswerRe: How to set project for always support hots key ? Pin
Synetech22-Jul-06 7:34
MemberSynetech22-Jul-06 7:34 
Generalerror C2039: 'ProcessMessageFilter' : is not a member of 'CWinApp' Pin
atrung5-Jun-06 18:53
Memberatrung5-Jun-06 18:53 
General1000 thx! Pin
chris mook11-Apr-06 4:02
Memberchris mook11-Apr-06 4:02 
QuestionHow to do this to a dialog under a child window? Pin
kezhu31-Mar-06 12:34
Memberkezhu31-Mar-06 12:34 
QuestionHow to call a current Function now? Pin
bosfan11-Jan-06 3:00
Memberbosfan11-Jan-06 3:00 
AnswerRe: How to call a current Function now? Pin
bosfan11-Jan-06 4:57
Memberbosfan11-Jan-06 4:57 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.