Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Edit control capture enter key

Tags:

c++

winapi

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.

like image 820
Johnny Mnemonic Avatar asked Mar 29 '13 20:03

Johnny Mnemonic


2 Answers

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);
}
like image 178
W.B. Avatar answered Nov 19 '22 00:11

W.B.


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);
}
like image 20
Danil Avatar answered Nov 18 '22 23:11

Danil