Most of the time, you need to click a button after entering the parameter to perform some operations. More people are used to hitting the Enter key on the keyboard to respond to the event.
So how to respond to the Return key?
Of course, if we use the system's default OK key in MFC as the response key, no additional settings are needed. However, we often have a response to the manually added button.
Search for example:
Enter a keyword in the text box and press the Enter key to respond to the filter key.
In essence: The response Keyboard Message is to reload the pretranslatemessage () function.
Bool ctestdlg: pretranslatemessage (MSG * PMSG)
{
// Todo: Add dedicated code and/or call the base class here
Switch (PMSG-> message)
{
Case wm_keydown:
Switch (PMSG-> wparam)
{
// Process the Escape key message
Case vk_escape:
Return true;
Case vk_return:
If (: getdlgctrlid (PMSG-> hwnd) = idc_edit1) // text box ID: idc_edit1
{// Button ID: idc_button1
Postmessage (wm_command, makewparam (idc_button1, bn_clicked), (lparam) getdlgitem (idc_button1)-> m_hwnd );
}
Return true;
Default:
Break;
}
Break;
Default:
Break;
}
Return cdialog: pretranslatemessage (PMSG );
}