NO

Author Topic: how to capture Enter key for Edit Box in win32api  (Read 3691 times)

rp108

  • Guest
how to capture Enter key for Edit Box in win32api
« on: December 15, 2015, 04:14:27 AM »
I have a win32api Dialog based app.

I have 2 Edit boxes and 1 Button.

You enter text in Edit 1 and Press the Button. The text is copied to edit 2
This is working.

I would like to enter text into Edit 1 and Press the ENTER key and have it copied to Edit 2.

I have tried a bunch of approaches from testing focus (GetFocus) and causing the copy, to, detecting WM_KEYDOWN and causing copy that way.
I have not gotten any of it to work.

After a couple of days I am here to ask the community :-)

I have zipped up an example project that has precisely the components I need.

Any help is much appreciated.

Thanks,
Rob

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: how to capture Enter key for Edit Box in win32api
« Reply #1 on: December 15, 2015, 09:24:33 AM »
Maybe this help:
Code: [Select]
HWND g_OK;
...
case WM_INITDIALOG:
   g_OK = GetDlgItem(hwndDlg, IDOK);
  return TRUE;
  ...
case IDOK:
  if (GetFocus()== g_OK) EndDialog(hwndDlg, TRUE);
  else MessageBox(hwndDlg, "Enter", "Enter Detected", MB_OK | MB_ICONINFORMATION);
or
Code: [Select]
case IDOK: {
  int iCtlId = GetDlgCtrlID(GetFocus());
  // int iCtlId = GetWindowLongPtr(GetFocus(), GWLP_ID);
  if (iCtlId == IDOK) { EndDialog(hwndDlg, TRUE); return TRUE; }
  ...
}
« Last Edit: December 15, 2015, 11:35:58 AM by TimoVJL »
May the source be with you

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: how to capture Enter key for Edit Box in win32api
« Reply #2 on: December 15, 2015, 12:51:09 PM »
Another example using subclassing.
The lower edit control has also the 'want_return' and 'multiline' flags enabled.
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

rp108

  • Guest
Re: how to capture Enter key for Edit Box in win32api
« Reply #3 on: December 16, 2015, 05:14:49 AM »
Timo and Frankie,

Many thanks to both of you for assisting me - again :-)

This is the best support forum I have used in the last 15 years.

Timo,
I almost got it working with your 1st method.

Actually  have the it working to transfer the text, but I can't quit the application.
When I click the OK button, nothing happens.

As I understand it ....

So in the  WM_INITDIALOG, i get the Handle to the IDOK item number, using,
 g_OK = GetDlgItem(hwndDlg, IDOK);

Then when the IDOK signal comes along, I test to see if it came from my Edit box
or not, using,
if (GetFocus()== g_OK) EndDialog(hwndDlg, TRUE);

So if it did come from the OK (closing) Button
then GetFocus() will equal g_OK
then I quit the app,
otherwise GetFocus() is for my Edit box,
and I do something else.

Not sure why it does not work ... but it doesn't matter.

I did indeed get your second way to work in all respects.
It's cool since I have not used GetDlgCtrlID() before and now I have a new tool.
Here is what I ended up with:

case IDOK:

   iCtlId = GetDlgCtrlID(GetFocus());
      
if (iCtlId == IDOK)
{
EndDialog(hwndDlg, TRUE);
return TRUE;
}
else
{
// Get the text to send from the edit box 4001
Edit_GetText(GetDlgItem(hwndDlg,4001), gbuf, gbufmx);

//update the Edit box 4002 with the text from 4001
Edit_SetText(GetDlgItem(hwndDlg,4002), gbuf);
return TRUE;
}


} //end switch (GET_WM_COMMAND_ID(wParam, lParam))


Again, many thanks to you and Frankie.

Best regards,
Rob





rp108

  • Guest
Re: how to capture Enter key for Edit Box in win32api
« Reply #4 on: December 17, 2015, 05:00:13 AM »
Hi Timo,

I am not sure if you are still watching this thread ......

When I took the code and put it into my app, the Enter Key just caused the app to close.

So when I studied the differences between the sample app I included, with your code, and my app, the difference is that I use a multiline Edit box in my app.

So I changed the little same app Edit boxes to multiline and sure enough the app closes when you hit the Enter key, right after entering text.

I added a little debug code that writes out a text file to HD to compare the integer value of IDOK and whatever GetDlgCtrlID(GetFocus()) is returning.

When the Edit box is single line, you get the correct numeric value of the Edit box, but ..
When the Edit box is multiline, you get  1  for the  GetDlgCtrlID(GetFocus()) value.

Got any ideas on this how to fix this?

I zipped up the sample app with multiline, just in case you have time to look at it.

Many thanks and the best to you,
Rob



Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: how to capture Enter key for Edit Box in win32api
« Reply #5 on: December 17, 2015, 11:21:02 AM »
With multiline use:
ES_MULTILINE | ES_WANTRETURN

You need those for editing anyway.

Now you can cleanup case IDOK.
May the source be with you

rp108

  • Guest
Re: how to capture Enter key for Edit Box in win32api
« Reply #6 on: December 17, 2015, 06:18:04 PM »
Thanks Timo  :)