In MFC, overriding virtual functions in PreTranslateMessage can filter wm_keydown messages. However, if you are using Dialogbox/dialogboxparam to create modal dialogs in the Win32 SDK, the general behavior is not to process WM_KEYDOWN messages.If there are no child controls in the dialog box, the window callback function Dlgproc () in the dialog box can be filtered to the WM_KEYDOWN message., but not all of the key messages can be captured, such as the TAB key, the upper and lower left and right arrow keys vk_up/vk_down/vk_left/vk_right and so on have no way to capture.
If there is a child control in the modal dialog box, even if it is a static text control, there is no way to get any wm_keydown message in the window callback function of the dialog box.
So the modal dialog box can use the following method if it wants to handle the TAB key/vk_up/down/left/rigth without any child controls:
INT_PTR CALLBACK Dlgproc (HWND hdlg, UINT message, WPARAM WPARAM, LPARAM LPARAM) {unreferenced_parameter (LPARAM); switch ( Message) {Case wm_initdialog:{oldwndproc = (WNDPROC) SetWindowLong (Hdlg, GWL_WNDPROC, (LONG) newdlgproc);// Sets the new window procedure callback function}return (INT_PTR) TRUE;} // ... Other message processing, here omit return (INT_PTR) FALSE;} LRESULT CALLBACK Newdlgproc (HWND hwnd, UINT message, WPARAM WPARAM, LPARAM LPARAM)//The dialog box has a new window callback function that filters the WM_KEYDOWN messages. {switch (message) {case wm_getdlgcode:{ return (Dlgc_wantallkeys | CallWindowProc (Oldwndproc, hWnd, message, WParam, LParam)); Note here, otherwise there is no way to capture the tab/direction key}case Wm_keydown:{tchar Sztext[max_path] = {0}; stringcchprintf (Sztext, _countof (Sztext), _t ("%d"), WParam); SetWindowText (HWnd, sztext);} Break;default:break;} Return CallWindowProc (Oldwndproc, hWnd, message, WParam, LParam);}
Add: If there are other child controls on the dialog box, use the method and its type, subclass, set the control's new window callback function, and the new window callback function is similar to the one above. For example, there is a static text control in the dialog box, which is how to capture the tab/arrow key event.
Win32 Dialog dialog box handles WM_KEYDOWN events