How to get focused gadget?
How to get focused gadget?
Is possible to get the focused gadget without manually check every one?
Re: How to get focused gadget?
Maybe theres a way to do it with BindEvent() & BindGadgetEvent()
Anyway try this (Windows):
Anyway try this (Windows):
Code: Select all
EnableExplicit
Procedure.i GetHwndWithFocus(hWnd.i)
Protected tid.i
Protected gti.GUITHREADINFO
gti\cbSize = SizeOf(GUITHREADINFO)
If GetGUIThreadInfo_(GetWindowThreadProcessId_(hWnd,@tid),@gti)
ProcedureReturn gti\hwndFocus
EndIf
ProcedureReturn #Null
EndProcedure
Procedure.i Main()
Protected wnd.i
Protected wnd_event.u
Protected wnd_exit.i
wnd = OpenWindow(#PB_Any,#Null,#Null,300,200,#Null$,#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
If wnd
StringGadget(0,10,10,100,20,#Null$);<- click with the mouse into the string gadgets to see if it matches the debugger output :)
StringGadget(1,10,40,100,20,#Null$)
SetGadgetText(0,Str(GadgetID(0)))
SetGadgetText(1,Str(GadgetID(1)))
Repeat
Repeat
wnd_event = WindowEvent()
If wnd_event = #PB_Event_CloseWindow
Break 2
EndIf
Until wnd_event = #Null
Debug GetHwndWithFocus(WindowID(wnd))
Until wnd_exit
CloseWindow(wnd)
EndIf
ProcedureReturn #Null
EndProcedure
Main()
End
Re: How to get focused gadget?
RASHAD wrote:Hi
How aboutCode: Select all
GetActiveGadget()
True since it reports the focus and not the active state...
However active and focus are differnet states... so this is confusing!
It should be named correctly imho.
Re: How to get focused gadget?
@RASHAD, I think it's the function I need (I must read the entire functions of purebasic again )
@Mijikai, Your solution will be handy as well
Thanks both
@Mijikai, Your solution will be handy as well
Thanks both
Re: How to get focused gadget?
They are? What's the difference? They've always meant the same thing from what I've always read.Mijikai wrote:active and focus are differnet states
Also, your GetHwndWithFocus() procedure can be replaced with a single API command: GetFocus_().
Code: Select all
Procedure.i GetHwndWithFocus(hWnd.i)
Protected tid.i
Protected gti.GUITHREADINFO
gti\cbSize = SizeOf(GUITHREADINFO)
If GetGUIThreadInfo_(GetWindowThreadProcessId_(hWnd,@tid),@gti)
ProcedureReturn gti\hwndFocus
EndIf
ProcedureReturn #Null
EndProcedure
Procedure.i Main()
Protected wnd.i
Protected wnd_event.u
Protected wnd_exit.i
wnd = OpenWindow(#PB_Any,#Null,#Null,300,200,#Null$,#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
If wnd
StringGadget(0,10,10,100,20,#Null$);<- click with the mouse into the string gadgets to see if it matches the debugger output :)
StringGadget(1,10,40,100,20,#Null$)
SetGadgetText(0,Str(GadgetID(0)))
SetGadgetText(1,Str(GadgetID(1)))
Repeat
Repeat
wnd_event = WindowEvent()
If wnd_event = #PB_Event_CloseWindow
Break 2
EndIf
Until wnd_event = #Null
Debug Str(GetHwndWithFocus(WindowID(wnd)))+" / "+Str(GetFocus_())
Until wnd_exit
CloseWindow(wnd)
EndIf
ProcedureReturn #Null
EndProcedure
Main()
Re: How to get focused gadget?
Its different, something can be active but not have the focus.BarryG wrote:They are? What's the difference? They've always meant the same thing from what I've always read.Mijikai wrote:active and focus are differnet states
Also, your GetHwndWithFocus() procedure can be replaced with a single API command: GetFocus_().
GetFocus_() will only work for the current thread.
Re: How to get focused gadget?
Without API for all OS
Code: Select all
EnableExplicit
Enumeration CustomEvent #PB_Event_FirstCustomValue
#MyEvent_ChangeFocus
EndEnumeration
Structure udtInfo
Window.i
Focus.i
LastFocus.i
EndStructure
Global Info.udtInfo
Info\Window = -1
Info\Focus = -1
Procedure.i DoEventInfo()
Protected Focus = GetActiveGadget()
If Focus <> Info\Focus
Info\Window = EventWindow()
Info\LastFocus = Info\Focus
Info\Focus = Focus
PostEvent(#MyEvent_ChangeFocus, EventWindow(), Focus)
Debug "Focus = " + Info\Focus
EndIf
EndProcedure
BindEvent(#PB_Event_Repaint,@DoEventInfo())
Procedure.i Main()
If OpenWindow(0,100,100,300,200,"MyEvent",#PB_Window_SystemMenu)
StringGadget(0,10,10,100,20,"String 0")
StringGadget(1,10,40,100,20,"String 1")
ButtonGadget(2,10,70,100,25,"Button 2")
ButtonGadget(3,120,70,100,25,"Button 3")
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
Break
Case #PB_Event_Gadget
Select EventGadget()
Case 2
SetActiveGadget(0)
Case 3
SetActiveGadget(1)
EndSelect
Case #MyEvent_ChangeFocus
Debug "MyEvent ChangeFocus = " + EventGadget()
EndSelect
ForEver
EndIf
EndProcedure
Main()
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Re: How to get focused gadget?
Never heard of that before. Got an example?Mijikai wrote:something can be active but not have the focus.
Re: How to get focused gadget?
Example:BarryG wrote:Never heard of that before. Got an example?Mijikai wrote:something can be active but not have the focus.
Code: Select all
Procedure.i GetHwndWithFocus(hWnd.i)
Protected tid.i
Protected gti.GUITHREADINFO
gti\cbSize = SizeOf(GUITHREADINFO)
If GetGUIThreadInfo_(GetWindowThreadProcessId_(hWnd,@tid),@gti)
Debug gti\hwndActive ;<- this is not the same as this -> gti\hwndFocus!
ProcedureReturn gti\hwndFocus
EndIf
ProcedureReturn #Null
EndProcedure
Re: How to get focused gadget?
I looked it up and discovered hwndActive is for the actual window itself being active, and hwndFocus is for any window (gadget in PureBasic) on that window that has the keyboard focus. So in PureBasic, hwndActive = GetActiveWindow(), and hwndFocus = GetActiveGadget().
Re: How to get focused gadget?
GetActiveWindow() and GetActiveGadget do not return the window handle hWnd, but the Purebasic internal PB-ID.
To get the PB-ID via the hWnd, see Module System
To get the PB-ID via the hWnd, see Module System
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Re: How to get focused gadget?
<Slaps forehead> Of course. I should've mentioned to use WindowID() and GadgetID() with them as well.mk-soft wrote:GetActiveWindow() and GetActiveGadget do not return the window handle hWnd, but the Purebasic internal PB-ID.
hwndActive = WindowID(GetActiveWindow())
hwndFocus = GadgetID(GetActiveGadget())
And before anyone says it: you should check that GetActive... is not -1 before using with WindowID()/GadgetID().