Jump to content
  • Advertisement
Sign in to follow this  
CTEagle

Using the "Enter" key

This topic is 4110 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a basic chat application. It contains an edit box where you type in the message that you want to send. There is also a "Send" button that actually sends the message contained in the edit box. When I finish typing my message in the edit box, I want to be able to hit the "Enter" key and have my program execute the same code as when I hit the "Send" button. I only want to be able to do this when the edit box has focus. The "Enter" key should not have any effect when any other object on my form is selected. I am using C++\WINAPI only. Thanks in advance for any help.

Share this post


Link to post
Share on other sites
Advertisement
If you make the send button the default button on the dialog then pressing enter will behave as if the user pressed that button.

Alternatively, you can subclass the edit control to handle the case when enter is pressed and then do whatever pressing the send button does.

Share this post


Link to post
Share on other sites
Since you want the Enter key only to work when the edit is focussed you have to resort to subclassing (in MFC you'd use PreTranslateMessage).

Subclass the edit and listen to WM_KEYDOWN with VK_RETURN.

Subclassing is done via SetWindowLong/SetWindowLongPtr and GWL_WNDPROC. Do not forget to CallWindowProc the original WndProc pointer you get as return value from SetWindowLong/SetWindowLongPtr.

Share this post


Link to post
Share on other sites
Thanks everyone for your replies.

Hobbesian - I am using Visual C++.

Endurian - Thanks for the info on subclassing. I have never used subclassing before so I am off to google to learn more about it. Any information that you could supply in addition to what you have already posted would be greatly appreciated.

Share this post


Link to post
Share on other sites
It's pretty simple once you get the hang of it:

You prepare your own EditProc which looks like a standard WindowProc. Prepare a WNDPROC variable to hold the original edit's WndProc.

Do this:


WNDPROC g_oldEditProc = NULL;

LRESULT CALLBACK MyEditProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{

if ( uMsg == WM_KEYDOWN )
{
if ( wParam == VK_RETURN )
{
// do action
}
}

// pass messages on to edit
return CallWindowProc( g_oldEditProc, hWnd, uMsg, wParam, lParam );

}

// your init code
g_oldEditProc = (WNDPROC)::SetWindowLong( hwndEdit, GWL_WNDPROC, MyEditProc );

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!