Casebash
Casebash

Reputation: 118962

How do I handle a key press in MFC?

I want to intercept the keys that are pressed when one of my dialogs is displayed

Upvotes: 5

Views: 20171

Answers (2)

Goz
Goz

Reputation: 62333

Either set up some accelerators that send the relevant WM_COMMAND or respond to WM_KEYDOWN/UP messages and look out for your key presses.

More info here:

http://www.codeproject.com/KB/dialog/pretransdialog01.aspx

And the useful code from that article:

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

Upvotes: 4

Paul Sasik
Paul Sasik

Reputation: 81517

You should look into hooking keyboard events. That's a CodeGuru link but there are many, many resources available when you google for How to hook keyboard events with MFC.

What you'll find in those articles is that keyboard and/or event hooking is the terminology of choice. "Intercept" is rately used to describe the process. And one more note: Be careful where you hook! You can hook into your dialog or at several other levels. (Btw, key loggers are well-known trojans that employ this tactic. Just an FYI.)

Upvotes: 1

Related Questions