Click here to Skip to main content
15,904,287 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

i am using a CEdit control single line inwhich i need to process the data only on enter key hit, currently i have catch the enter hit by using the WM_KEYDOWN in the PreTranslateMessage and it works but i am catching the OnKillFocus event too so when the control loses focus i can reset the text to some default text i have.

Problem is that the kill focus message always comes first that the WM_KEYDOWN message with the enter key, how can i make the kill focus event ocurr when user hits enter?


this is the code i am using now when user hits ENTER WM_KEYDOWN gets generated but at the same time OnKillFocus is called.

BOOL CEditExt::PreTranslateMessage(MSG* pMsg)
    if( pMsg->message == WM_KEYDOWN && (pMsg->wParam == VK_ESCAPE || pMsg->wParam == VK_RETURN || pMsg->wParam == VK_TAB))
        bool abortEdit = false;
        if(pMsg->wParam != VK_RETURN)
            abortEdit = true;

        if(abortEdit == true) return 0;
    return CEdit::PreTranslateMessage(pMsg);

void CEditExt::OnKillFocus(CWnd* pNewWnd)


void CEditExt::EndEdit(bool cancelEdit)

	CString text;

	InfoEdit info;			

	::SendMessage(  this->GetParent()->GetSafeHwnd(), M_ENDEDIT, (WPARAM)&info, cancelEdit );


Oh no!, i just figured out that the call
was calling the killfocus event so i just move that call down
and is working now

Updated 5-Oct-11 12:26pm
Sergey Alexandrovich Kryukov 5-Oct-11 17:45pm    
What is "kill focus"? I don't know such thing.
manchukuo 5-Oct-11 17:49pm    
the WM_KILLFOCUS message i mean
André Kraak 5-Oct-11 17:51pm    
Please share any relevant code with us, seeing the code might us help understand the problem you are facing.

If you wish to change your question use the Improve Question button.

Set ES_WANTRETURN using the SetWindowLong function:
Edit Control Styles[^]

Best regards
Espen Harlinn
Share this answer
manchukuo 5-Oct-11 18:31pm    
that will only would had worked if i were using the multiline option of CEdit,
thanks for the try i had already fixed the problem :)
I am not entirely sure I understand your problem.

But I do known that the statement ShowWindow(SW_HIDE); will cause the control to lose its focus.
Try commenting out this line as see if it works as expected.
Share this answer
manchukuo 5-Oct-11 18:27pm    
yes Andre i figured just minutes before your post i just move down that call and that is all, i will give you this answer to you if that is ok with you
André Kraak 5-Oct-11 18:37pm    
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900