I am trying to catch ENTER and ESC key press in singleline edit control.
When user presses ENTER or ESC I want to take away keyboard focus from edit control and set it to listview control. Listview control is edit control's sibling.
My goal is to write single subclass procedure that can be used for subclassing edit controls in both main window and dialog box.
I have found this MSDN article that I found useful because of its second solution. Below is my adaptation of the code.
// subclass procedure for edit control
LRESULT CALLBACK InPlaceEditControl_SubclassProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam,
UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
{
switch (message)
{
case WM_GETDLGCODE:
return (DLGC_WANTALLKEYS | DefSubclassProc(hwnd, message, wParam, lParam));
case WM_CHAR:
//Process this message to avoid message beeps.
switch (wParam)
{
case VK_RETURN:
// change focus to listview
SetFocus(hwndListView);
return 0L;
case VK_ESCAPE:
// change focus to listview
SetFocus(hwndListView);
return 0L;
default:
return ::DefSubclassProc(hwnd, message, wParam, lParam);
}
break;
case WM_KEYDOWN:
switch (wParam)
{
case VK_RETURN:
// change focus to listview
SetFocus(hwndListView);
return 0L;
case VK_ESCAPE:
// change focus to listview
SetFocus(hwndListView);
return 0L;
default:
return ::DefSubclassProc(hwnd, message, wParam, lParam);
}
break;
case WM_NCDESTROY:
::RemoveWindowSubclass(hwnd, InPlaceEditControl_SubclassProc, uIdSubclass);
return DefSubclassProc(hwnd, message, wParam, lParam);
}
return ::DefSubclassProc(hwnd, message, wParam, lParam);
}
QUESTION:
Is my adaptation correct or am I missing something (maybe instead of SetFocus
I should use WM_NEXTDLGCTL
like Raymond Chen pointed out)?
SetFocus
in a dialog will fail to update its internal state. This will lead to situations, where two button controls appear to be the default button at the same time. Since the documentation forWM_NEXTDLGCTL
doesn't state that it can be sent to non-dialog windows, you'd have to write conditional code (that's what a standard edit control does). – CollieWM_NEXTDLGCTL
is applicable to non-dialog windows, I asked a question. If the answer is, that you can use the message with any window, you'd simply have to replace yourSetFocus
-calls (and remove those in yourWM_CHAR
handler; this handler prevents message beeps due to theDLGC_WANTALLKEYS
flag). – CollieDLGC_WANTALLKEYS
, you have to deal with keys that the standard edit control doesn't normally receive, namelyVK_RETURN
andVK_ESCAPE
. You already updated input focus in yourWM_KEYDOWN
-handler, so you can simply ignore the keys in yourWM_CHAR
-handler, like the reference code does. No need to update the input focus again. Not trying to disappoint you, but there is no such thing as someone like Raymond Chen, much in the same way as there is no such thing as someone like Cuck Norris. – CollieWM_NEXTDLGCTL
doesn't work in normal window. Again, I apologize if I offended you in any kind. – Heartbreaking