Which gadget number is lost?

Just starting out? Need help? Post your questions and find answers here.
User avatar
charvista
Addict
Addict
Posts: 949
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

Which gadget number is lost?

Post by charvista »

Hi

We have StickyWindow() to keep a window always on top. OK.
But if the focus get lost because the user tried to swap to another app or clicked elsewhere, how to force to keep the focus as well? The answer is probably SetActiveWindow() followed with SetActiveGadget(), but then how can I know which gadget number is lost?

Thanks.
- Windows 11 Home 64-bit
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Which gadget number is lost?

Post by PB »

This works if you click the gadgets, but not if you tab between them,
because ButtonGadgets don't support #PB_EventType_Focus. :shock:

Code: Select all

If OpenWindow(0,300,250,400,200,"Button focus",#PB_Window_SystemMenu)
  ButtonGadget(1,50,50,60,25,"1")
  ButtonGadget(2,150,50,60,25,"2")
  ButtonGadget(3,250,50,60,25,"3")
  StringGadget(4,50,100,260,21,"4")
  CheckBoxGadget(5,50,130,260,21,"5")
  SetActiveGadget(1)
  oldag=1
  Repeat
    ev=WaitWindowEvent()
    If ev=#PB_Event_Gadget
      ag=GetActiveGadget()
      If ag<>oldag
        Debug Str(oldag)+" just lost focus"
        oldag=ag
      EndIf
    EndIf
  Until ev=#PB_Event_CloseWindow
EndIf
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
charvista
Addict
Addict
Posts: 949
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

Re: Which gadget number is lost?

Post by charvista »

Thank you PB. It is an excellent start :)

You are right, TAB between them does not work on all gadgets. To solve it completely, I also need to trap the TAB key between the gadgets as well.
So how to solve the problem below, so the variable Ag always reflects the current active gadget ?

Code: Select all

If OpenWindow(0,300,250,400,200,"Button focus",#PB_Window_SystemMenu)
    StickyWindow(0,1); make it top window
    
    ButtonGadget(1,50,50,60,25,"1")
    ButtonGadget(2,150,50,60,25,"2")
    ButtonGadget(3,250,50,60,25,"3")
    StringGadget(4,50,100,260,21,"4")
    CheckBoxGadget(5,50,130,260,21,"5")
    TextGadget(6,50,170,350,20,"")
    
    Ag=1
    SetActiveGadget(Ag)
    SetGadgetText(6,"Current active gadget: "+Str(Ag))
    
    Repeat
        Event=WaitWindowEvent(50)
        Select Event
            
        Case #PB_Event_CloseWindow
            ExitEventLoop=#True
            
        Case #PB_Event_Gadget
            Gadget=EventGadget()      
            EvType=EventType()
            Debug Str(Gadget)+"    "+Str(EvType)
            
            
            If GetActiveWindow()<>Win
                SetActiveWindow(Win)
                SetActiveGadget(Ag)
            EndIf
            
            Select Gadget
                
            Case 1,2,3,4,5
                Ag=Gadget
                SetGadgetText(6,"Current active gadget: "+Str(Ag))
                
            EndSelect
        EndSelect
    Until ExitEventLoop
EndIf
- Windows 11 Home 64-bit
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Which gadget number is lost?

Post by Little John »

PB wrote:This works if you click the gadgets, but not if you tab between them, [...]
This works also when using the TAB keys (tested with PB 5.21 LTS on Windows XP x86):

Code: Select all

OpenWindow(0, 300, 250, 400, 200, "Button focus", #PB_Window_SystemMenu)
ButtonGadget(1, 50, 50, 60, 25, "1")
ButtonGadget(2, 150, 50, 60, 25, "2")
ButtonGadget(3, 250, 50, 60, 25, "3")
StringGadget(4, 50, 100, 260, 21, "4")
CheckBoxGadget(5, 50, 130, 260, 21, "5")
SetActiveGadget(1)
oldag = 1

Repeat
   ev = WaitWindowEvent()
   ; If ev = #PB_Event_Gadget
      ag = GetActiveGadget()
      If ag <> oldag
         Debug Str(oldag) + " just lost focus"
         oldag = ag
      EndIf
   ; EndIf
Until ev = #PB_Event_CloseWindow
User avatar
charvista
Addict
Addict
Posts: 949
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

Re: Which gadget number is lost?

Post by charvista »

Thanks Little John!!
Checking for the current active gadget before checking the #PB_Event_Gadget does the trick!

:wink:
- Windows 11 Home 64-bit
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Which gadget number is lost?

Post by Little John »

charvista wrote:Checking for the current active gadget before checking the #PB_Event_Gadget does the trick!
Exactly.
The credits go to Fluid Byte, who explained it to me some years ago. :-)
User avatar
charvista
Addict
Addict
Posts: 949
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

Re: Which gadget number is lost?

Post by charvista »

Even when having some experience, we are all still learning!
So, Vielen Dank to Fluid Byte as well :D
- Windows 11 Home 64-bit
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4947
Joined: Sun Apr 12, 2009 6:27 am

Re: Which gadget number is lost?

Post by RASHAD »

Get the focused Gadget with Mouse click or Tab Key
Not so perfect I guess :P

Code: Select all

  If OpenWindow(0, 0, 0, 355, 300, "TreeGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    ButtonGadget(1,50,50,60,25,"1")
    ButtonGadget(2,150,50,60,25,"2")
    ButtonGadget(3,250,50,60,25,"3")
    StringGadget(4,50,100,260,21,"4")
    CheckBoxGadget(5,50,130,260,21,"5")
    TextGadget(6,50,170,350,20,"")
  
  Repeat
 
    Select WaitWindowEvent()
      Case #WM_LBUTTONDOWN,#WM_LBUTTONUP
            Debug "Gadget : "+Str(GetProp_(GetFocus_(), "PB_ID")) + " Got The Focus"
           
      Case #WM_KEYUP
           If EventwParam() = 9
                Debug "Gadget : "+Str(GetProp_(GetFocus_(), "PB_ID")) + " Got The Focus"
            EndIf           
    
      Case #PB_Event_CloseWindow
            Quit = 1
       
    EndSelect
  Until Quit = 1
  EndIf

Egypt my love
User avatar
charvista
Addict
Addict
Posts: 949
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

Re: Which gadget number is lost?

Post by charvista »

Excellent, RASHAD, my old coffee-friend :mrgreen:
Your approach is interesting because it is inside a #WM_ event. I will very probably use your idea because it is safe and Win-API is not a problem.
My focus problem goes together with the focus of the window, that you can see at http://www.purebasic.fr/english/viewtop ... 72#p432246
Can you please take a look there?

Thanks! And like last year, I'll probably be awake all night :mrgreen:
- Windows 11 Home 64-bit
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Which gadget number is lost?

Post by PB »

Just a side-tip: you can replace "Case 1,2,3,4,5" with "Case 1 To 5". :)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
charvista
Addict
Addict
Posts: 949
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

Re: Which gadget number is lost?

Post by charvista »

PB wrote:Just a side-tip: you can replace "Case 1,2,3,4,5" with "Case 1 To 5". :)
:wink:
- Windows 11 Home 64-bit
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Which gadget number is lost?

Post by PB »

> how to force to keep the focus as well?

The solution, made from all tips above:

Code: Select all

OpenWindow(0, 300, 250, 400, 200, "Button focus", #PB_Window_SystemMenu)

ButtonGadget(1, 50, 50, 60, 25, "1")
ButtonGadget(2, 150, 50, 60, 25, "2")
ButtonGadget(3, 250, 50, 60, 25, "3")
StringGadget(4, 50, 100, 260, 21, "4")
CheckBoxGadget(5, 50, 130, 260, 21, "5")

keepag = 1
SetActiveGadget(keepag)

Repeat
   ev = WaitWindowEvent()
   ; If ev = #PB_Event_Gadget
      ag = GetActiveGadget()
      If ag <> keepag
        SetActiveGadget(keepag)
      EndIf
   ; EndIf
Until ev = #PB_Event_CloseWindow
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Post Reply