开发者

How do I handle a key press in MFC?

I want to intercept the keys th开发者_如何学运维at are pressed when one of my dialogs is displayed


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);
}


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.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜