Which gadget number is lost?
Which gadget number is lost?
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.
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%
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
Re: Which gadget number is lost?
This works if you click the gadgets, but not if you tab between them,
because ButtonGadgets don't support #PB_EventType_Focus.
because ButtonGadgets don't support #PB_EventType_Focus.

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.
"PureBasic won't be object oriented, period" - Fred.
Re: Which gadget number is lost?
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 ?

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%
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
-
- Addict
- Posts: 4779
- Joined: Thu Jun 07, 2007 3:25 pm
- Location: Berlin, Germany
Re: Which gadget number is lost?
This works also when using the TAB keys (tested with PB 5.21 LTS on Windows XP x86):PB wrote:This works if you click the gadgets, but not if you tab between them, [...]
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
Re: Which gadget number is lost?
Thanks Little John!!
Checking for the current active gadget before checking the #PB_Event_Gadget does the trick!

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

- 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%
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
-
- Addict
- Posts: 4779
- Joined: Thu Jun 07, 2007 3:25 pm
- Location: Berlin, Germany
Re: Which gadget number is lost?
Exactly.charvista wrote:Checking for the current active gadget before checking the #PB_Event_Gadget does the trick!
The credits go to Fluid Byte, who explained it to me some years ago.

Re: Which gadget number is lost?
Even when having some experience, we are all still learning!
So, Vielen Dank to Fluid Byte as well
So, Vielen Dank to Fluid Byte as well

- 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%
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
Re: Which gadget number is lost?
Get the focused Gadget with Mouse click or Tab Key
Not so perfect I guess
Not so perfect I guess

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
Re: Which gadget number is lost?
Excellent, RASHAD, my old coffee-friend
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

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

- 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%
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
Re: Which gadget number is lost?
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.
"PureBasic won't be object oriented, period" - Fred.
Re: Which gadget number is lost?
PB wrote:Just a side-tip: you can replace "Case 1,2,3,4,5" with "Case 1 To 5".

- 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%
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
Re: Which gadget number is lost?
> how to force to keep the focus as well?
The solution, made from all tips above:
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.
"PureBasic won't be object oriented, period" - Fred.