VC ++ Dialog Box program response Keyboard Message Processing Method (very important), vc Processing Method

Source: Internet
Author: User

VC ++ Dialog Box program response Keyboard Message Processing Method (very important), vc Processing Method

The application based on the MFC dialog box cannot respond to the pressing messages and hotkeys. The TranslateAccelerator function is removed from the message loop of the CDialog class, so it cannot respond to hot keys. At the same time, there may be many widgets in the dialog box, by default, these subwindows have captured the focus, so the Keyboard Message has been captured by the control. To achieve the control focus switching and the default dialog box behavior, keys such as VK_TAB, VK_LEFT, VK_RIGHT, VK_UP, VK_DOWN, VK_RETURN, and VK_ESCAPE have been intercepted and processed. Therefore, when there is no control in the dialog box, keys cannot be completely responded.

For the hotkey response, see this article: Keyboard messages/accelerators handling in MFC dialog based applications. So how can we make the dialog box program respond to the key message? The answer is to overload PreTranslateMessage and intercept keyboard messages for processing. There are about four KEY-related messages: WM_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN, and WM_SYSKEYUP. The following information must be noted: 1. The difference between keys and SYSKEY messages is as follows: if the ALT or F10 KEY is pressed at the same time as a KEY action, the SYSKEY message is sent; otherwise, the KEY message is sent; 2. the WM_KEYDOWN and WM_KEYUP messages are generated in sequence by a KEY action; 3. If a key is pressed for a long time, the WM_KEYDOWN message will be sent continuously at a certain interval. 4. It is best for a single-key action to respond to WM_KEYUP, and for a combination key action to respond to WM_KEYDOWN or WM_SYSKEYDOWN; for detailed explanations of several key messages, refer to MSDN. The following is a sample code for responding to a single-key message:
BOOL CMyDlg: PreTranslateMessage (MSG * pMsg) {if (pMsg-> message = WM_KEYUP) {// response to the keyup message if (pMsg-> wParam = VK_RETURN) {// press ENTER} return CDialog: PreTranslateMessage (pMsg );}

 

How can we determine the key combination? Use the GetKeyState function. The sample code is as follows:
BOOL CMyDlg: PreTranslateMessage (MSG * pMsg) {if (pMsg-> message = WM_KEYDOWN) {// key combination response keydown message if (pMsg-> wParam = VK_SPACE & (GetKeyState (VK_SHIFT) & 0x8000 )) {// space + Shift} else if (pMsg-> message = WM_SYSKEYDOWN) {// Alt combination key response syskeydown message if (pMsg-> wParam = 'A' & (HIWORD (pMsg-> lParam) & KF_ALTDOWN )) {// A + Alt} return CDialog: PreTranslateMessage (pMsg );}

 

In addition, there are similar functions such as GetAsyncKeyState and GetKeyboardState, which involve logical keys and physical key values. For details, refer to the description of MSDN.

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.