I have an edit control
HWND hInput = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "",
WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_MULTILINE | ES_AUTOVSCROLL,
0, 0, 100, 100, hwnd, (HMENU)IDC_MAIN_INPUT, GetModuleHandle(NULL), NULL);
and a button:
HWND hSendButton = CreateWindowEx(WS_EX_CLIENTEDGE,"BUTTON","Send!",
BS_DEFPUSHBUTTON | WS_VISIBLE | WS_CHILD,
0,0,0,0,hwnd,(HMENU)IDC_MAIN_SENDBUTTON,GetModuleHandle(NULL),NULL);
Is there a way to see when the Enter key is pressed while typing in the edit control and send a message like the button was pressed?
I think that maybe
SendMessage(hwnd,WM_COMMAND,(WPARAM)IDC_MAIN_SENDBUTTON,LPARAM(0));
would do the job for sending the message but I'm still stuck at capturing the Enter key.
Thank you in advance.
You need to subclass the edit control and handle WM_KEYDOWN message. If it's the key you want, send the message, otherwise, let the default edit control procedure do its job.
The code would like this:
WNDPROC oldEditProc;
LRESULT CALLBACK subEditProc(HWND wnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_KEYDOWN:
switch (wParam)
{
case VK_RETURN:
//Do your stuff
break; //or return 0; if you don't want to pass it further to def proc
//If not your key, skip to default:
}
default:
return CallWindowProc(oldEditProc, wnd, msg, wParam, lParam);
}
return 0;
}
void somecreateeditproc()
{
HWND hInput = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "",
WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_MULTILINE | ES_AUTOVSCROLL,
0, 0, 100, 100, hwnd, (HMENU)IDC_MAIN_INPUT, GetModuleHandle(NULL), NULL);
oldEditProc = (WNDPROC)SetWindowLongPtr(hInput, GWLP_WNDPROC, (LONG_PTR)subEditProc);
}
I do it in dialog:
BOOL CDialogObject::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_RETURN)
{
SendMessage(WM_COMMAND, (WPARAM)IDC_BUTTON3,LPARAM(0));
}
return CDialog::PreTranslateMessage(pMsg);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With