If you press any of the keys on the keyboard, OnChar, OnKeyDown, onkeyup are called. The order in which they are called is:
1, OnKeyDown
2, OnChar
3, OnKeyUp
In a dialog application, the PreTranslateMessage function is always called, regardless of whether the user is manipulating the mouse or keyboard. The description of the PreTranslateMessage function in MSDN is as follows:
Used by class CWinApp to translate window messages before they is dispatched to the TranslateMessage and DispatchMessage Windows functions.
By default, the dialog application cannot respond to OnChar, onkeydown, and onkeyup events. Because the message that corresponds to it is intercepted by the control on the dialog box. The validation method is to remove all controls on the dialog box, at which point the OutputDebugString function does not add any breakpoints to debug the runner, and the dialog application can be found to respond to all OnChar, onkeydown, and onkeyup events. If we want to handle a specific keystroke message in a dialog application, you can overload the PreTranslateMessage function. For example, the following overloads the PreTranslateMessage function to mask the response of the dialog application to the "ESC" and "Enter" keys (the dialog application has a default button, which is the "OK" button, which can be modified programmatically.) When the user presses the ENTER key, it is the equivalent of pressing the default button, which is the OK button. In addition, when the user presses the "ESC" key, it is equivalent to pressing the "Cancel" button in the dialog box, which is still valid even if the OK or Cancel button has been deleted. )
BOOL Ctestdlg::P retranslatemessage (msg* pMsg)
{
//TODO: Add private code here and/or call the base class
if (Pmsg->wparam = = Vk_escape )
{
return true;
}
if (Pmsg->wparam = = Vk_return)
{
RETURN true;
}
/* When the letter a key is pressed, the wparam is initially 65, and then 97, regardless of the capitalization. That is, OutputDebugString will be called at least two times.
Add Breakpoint Debug OutputDebugString call two times, not add three times. (VS2008 tested) */
if (pmsg->wparam== ($) | | pmsg->wparam== (+))
{
OutputDebugString (_t ("wm_char:a \ n "))
;
Return CDialog::P retranslatemessage (PMSG);
}