dialog box keys handle PreTranslateMessage, onkeydown, and OnChar

Source: Internet
Author: User

dialog box keys handle PreTranslateMessage, onkeydown, and OnChar

1. MFC dialog box cannot respond to onkeydown and OnChar functions
(1) Phenomenon
In the MFC dialog box, the WM_CHAR and WM_KEYDOWN messages are mapped, but the dialog box does not respond to the onkeydown and OnChar functions
(2) Reason
Because MFC in the design, these two messages are intercepted by the control on the dialog box, can not reach the two message response function, for onkeydown, as long as the control on the dialog box is deleted, you can receive the WM_KEYDOWN message, but still do not receive WM_CHAR message
(3) Solve
Overload PreTranslateMessage this function; add SendMessage (Pmsg->message,pmsg->wparam,pmsg->lparam) in it; after this sentence, The onkeydown and OnChar Functions of the dialog box are in effect. OnKeyDown is processed before OnChar.

2.wm_keydown, Wm_keyup and WM_CHAR
The order of execution for three messages is Wm_keydown, WM_CHAR, Wm_keyup

3. In MFC's single document application, it extracts the key content directly.

4. If the direction key is handled, the following:

Arrow keys (←): Vk_left (37)
Arrow keys (↑): Vk_up (38)
direction keys (→): vk_right (39)
Arrow keys (↓): Vk_down (40)

Response Wm_keydown

1 void Cxxxxview::onkeydown (UINT NChar, uint nrepcnt, uint nflags) 2 {3     if (vk_left==NChar) 4     {5         MessageBox ("xxx"); 6     }7    cview::onkeydown (NChar, nrepcnt, nflags); 8 }   

The keys of the

dialog box handle PreTranslateMessage, onkeydown, and OnChar

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.