July 19, 2019, 01:29:06 AM

Author Topic: Can't maintain focus on a TextField when pressing Enter  (Read 316 times)

Offline TomToad

  • Sr. Member
  • ****
  • Posts: 385
Can't maintain focus on a TextField when pressing Enter
« on: May 15, 2019, 08:44:35 PM »
Trying to create a chat program.  Below is a scaled down version of the code.  Type into the text field and click send, the information is sent to the text area and focus is returned to the text field.  When you type something in, but press Enter instead of clicking, the text is still sent to the text area, but focus on the gadget is lost.  Even stranger, type in some text, but don't click Send or press Enter.  Instead, click outside of the text field to change focus to somewhere else, then press Enter.  You should still see the text being sent due to the BUTTON_OK style, but it isn't.  It is like the button is deactivated until you either click on it, or press Enter while the text area has focus.

I can't just simply do "If ActiveGadget() <> TextField Then ActivateGadget TextField", as this will affect interaction with other gadgets.
Code: BlitzMax
  1. SuperStrict
  2. Import maxgui.drivers
  3.  
  4. Global MainWindow:TGadget = CreateWindow("Test",0,0,600,600)
  5. Global TextArea:TGadget = CreateTextArea(0,0,ClientWidth(MainWindow),ClientHeight(MainWindow)-20,MainWindow,TEXTAREA_WORDWRAP|TEXTAREA_READONLY)
  6. Global TextField:TGadget = CreateTextField(0,ClientHeight(MainWindow)-20,ClientWidth(MainWindow)-80,20,MainWindow)
  7. Global SendButton:TGadget = CreateButton("Send",ClientWidth(MainWindow)-80,ClientHeight(MainWindow)-20,80,20,MainWindow,BUTTON_OK)
  8. AddHook EmitEventHook,HandleEvents
  9.  
  10. Repeat
  11.         WaitEvent()
  12. Forever
  13.  
  14. Function HandleEvents:Object(id:Int,data:Object,context:Object)
  15.         Local EVent:TEvent = TEvent(data)
  16.        
  17.         Select Event.id
  18.                 Case EVENT_WINDOWCLOSE, EVENT_APPTERMINATE
  19.                         End
  20.                 Case EVENT_GADGETACTION
  21.                         Select Event.source
  22.                                 Case SendButton
  23.                                         AddTextAreaText TextArea,GadgetText(TextField)+"~n"
  24.                                         SetGadgetText TextField,""
  25.                                         ActivateGadget TextField
  26.                                         Return Null
  27.                         End Select
  28.         End Select
  29.         Return data
  30. End Function
  31.  
  32.  

Edit:  I also tried adding a gadget filter to handle Enter there, but the Text Field gadget doesn't send Enter to the filter.
------------------------------------------------
8 rabbits equals 1 rabbyte.

Offline Henri

  • Full Member
  • ***
  • Posts: 203
Re: Can't maintain focus on a TextField when pressing Enter
« Reply #1 on: May 15, 2019, 10:32:05 PM »
Hi Tom,

if you don't mind editing MaxGui source a bit, there is this line in 'maxgui.mod\win32maxguiex.mod\win32maxguiex.bmx :
Code: BlitzMax
  1. Function KeyboardProc:Byte Ptr( code, wp:WParam, lp:LParam) "win32" nodebug
  2.                 Local ev:TEvent, hwnd:Byte Ptr, tmpClassName:Short[16], mods:Int, key:Int = wp
  3.                 If code>=0 Then
  4.                         'Removed: http://www.blitzbasic.com/Community/posts.php?topic=72737
  5. '                       Rem
  6.                         If wp = $D Then '$D: VK_RETURN
  7.                                 hwnd = GetFocus()
  8.                                 If hwnd And GetClassNameW(hwnd,tmpClassName,tmpClassName.length) And String.FromWString(tmpClassName).ToUpper() = "EDIT" Then
  9.                                         SetFocus(GetParent_(hwnd))
  10.                                 EndIf
  11.                         EndIf
  12. '                       EndRem

Un-comment the rem block and the behavior should be as you expect (just remember to build modules).

-Henri
- Got 01100011 problems, but the bit ain't 00000001

Offline TomToad

  • Sr. Member
  • ****
  • Posts: 385
Re: Can't maintain focus on a TextField when pressing Enter
« Reply #2 on: May 16, 2019, 01:41:13 AM »
looking here http://www.mojolabs.nz/posts.php?topic=72737, it appears that this behavior was intentional.  Since I am wanting to share my final code, modifying MaxGui is really not an option.  I did manage to make a workaround by using a TextArea and filtering for the Enter key.
Code: BlitzMax
  1. SuperStrict
  2. Import maxgui.drivers
  3.  
  4. Global MainWindow:TGadget = CreateWindow("Test",0,0,600,600)
  5. Global TextArea:TGadget = CreateTextArea(0,0,ClientWidth(MainWindow),ClientHeight(MainWindow)-20,MainWindow,TEXTAREA_WORDWRAP|TEXTAREA_READONLY)
  6. Global TextField:TGadget = CreateTextArea(0,ClientHeight(MainWindow)-20,ClientWidth(MainWindow)-80,20,MainWindow)
  7. SetGadgetFilter TextField,EnterFilter
  8. Global SendButton:TGadget = CreateButton("Send",ClientWidth(MainWindow)-80,ClientHeight(MainWindow)-20,80,20,MainWindow)
  9. AddHook EmitEventHook,HandleEvents
  10.  
  11. Function EnterFilter:Int(event:TEvent,context:Object)
  12.         If event.id = EVENT_KEYDOWN And event.data = 13
  13.                 AddLine()
  14.                 Return False
  15.         EndIf
  16.         Return True
  17. End Function
  18.  
  19. Function AddLine()
  20.         AddTextAreaText TextArea,GadgetText(TextField)+"~n"
  21.         SetGadgetText TextField,""
  22. End Function
  23.  
  24. Repeat
  25.         WaitEvent()
  26. Forever
  27.  
  28. Function HandleEvents:Object(id:Int,data:Object,context:Object)
  29.         Local EVent:TEvent = TEvent(data)
  30.        
  31.         Select Event.id
  32.                 Case EVENT_WINDOWCLOSE, EVENT_APPTERMINATE
  33.                         End
  34.                 Case EVENT_GADGETACTION
  35.                         Select Event.source
  36.                                 Case SendButton
  37.                                         AddLine()
  38.                                         ActivateGadget TextField
  39.                                         Return Null
  40.                         End Select
  41.         End Select
  42.         Return data
  43. End Function
  44.  
------------------------------------------------
8 rabbits equals 1 rabbyte.

Offline Henri

  • Full Member
  • ***
  • Posts: 203
Re: Can't maintain focus on a TextField when pressing Enter
« Reply #3 on: May 16, 2019, 08:04:00 AM »
Looking at that thread it seems that this behavior was originally requested by JoshK. Before that, it functioned as you would intuitively expect.

I believe that many resented this, but it was never 'fixed' back as Sebholl mentioned.

Personally, I prefer the expected behavior, where gadget doesn't lose focus and always fix it the way it suppose to be.

We could make this a default behavior?

-Henri
- Got 01100011 problems, but the bit ain't 00000001

Offline Derron

  • Hero Member
  • *****
  • Posts: 2208
Re: Can't maintain focus on a TextField when pressing Enter
« Reply #4 on: May 16, 2019, 08:13:31 AM »
@ Henri
Yes, MaxGUI is open source :-)

But instead of removing the current behaviour I would tend to add a flag/SetXYZBehaviour() function call. That way people could use the behaviour they "know" from other software or from MaxGUI.


@ shareable code
Can't you extend your code to have a "custom" widget with your behaviour?


bye
Ron