A description of how the VC + + dialog box responds to keyboard messages (very important)

Source: Internet
Author: User

MFC dialog-based applications are unable to respond to key messages and hotkeys, and the CDialog class's message loop removes the TranslateAccelerator function, so it cannot respond to hotkeys, and because there may be many controls on the dialog box, And by default these child windows have intercepted the focus, so the keyboard message has been captured by the control, and in order to implement the control focus switch and dialog box default behavior, Vk_tab, Vk_left, Vk_right, Vk_up, Vk_down, Vk_return, Vk_ The ESCAPE key has been intercepted for processing, so the dialog box does not have a control when it still cannot fully respond to the keystroke message.

refer to this article for a hotkey response:Keyboard messages/accelerators Handling in MFC dialog basedapplications.  So how do we get the dialog box to respond to a keystroke message? The answer is to overload the PreTranslateMessage, where the keyboard message is intercepted for processing. There are about 4 messages related to keystrokes: Wm_keydown, Wm_keyup, Wm_syskeydown, WM_SYSKEYUP, including the following points: 1, key and Syskey message difference is: If a key action, ALT key or F10 key is pressed, send SYSKEY message, otherwise send key message; 2, a key action in turn produces wm_keydown and wm_keyup messages; 3, a button has been pressed to keep, will be at a certain interval of time to send WM_KEYDOWN messages; 4, single-button action best response Wm_keyup, the combination of action response wm_keydown or wm_syskeydown; For a specific explanation of several key messages, please refer to MSDN. The following is a sample code that responds to a single-touch message:
BOOL CMyDlg::P retranslatemessage (msg* pMsg)   {if(pmsg->message = = wm_keyup) {/ /  response KeyUp message if(Pmsg->wparam = = Vk_return) {//  carriage return }} return CDialog::P retranslatemessage (PMSG);}

So how do you judge the key combinations? Use the Getkeystate function. The sample code is as follows:
BOOL CMyDlg::P retranslatemessage (msg*PMSG) {if(Pmsg->message = =wm_keydown) {//Composite Key Response KeyDown messageif(Pmsg->wparam = = vk_space&& (getkeystate (vk_shift) &0x8000)){//Space + Shift}}Else if(Pmsg->message = =Wm_syskeydown) {//ALT key combination Response Syskeydown messageif(Pmsg->wparam = ='A'&& (HiWord (Pmsg->lparam) &Kf_altdown)) {//A + Alt}} returnCDialog::P retranslatemessage (PMSG);}

In addition, there are similar functions such as getasynckeystate and getkeyboardstate, which involve logic keys and physical key values and so on, you can refer to the MSDN instructions.

A description of how the VC + + dialog box responds to keyboard messages (very important)

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.