The relationship between MFC OnChar, OnKeyDown, onkeyup, and PreTranslateMessage

Source: Internet
Author: User

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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.