How to get focused gadget?

Just starting out? Need help? Post your questions and find answers here.
User avatar
Caronte3D
Addict
Addict
Posts: 1051
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

How to get focused gadget?

Post by Caronte3D »

Is possible to get the focused gadget without manually check every one?
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: How to get focused gadget?

Post by Mijikai »

Maybe theres a way to do it with BindEvent() & BindGadgetEvent()

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
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4661
Joined: Sun Apr 12, 2009 6:27 am

Re: How to get focused gadget?

Post by RASHAD »

Hi
How about

Code: Select all

GetActiveGadget()
Egypt my love
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: How to get focused gadget?

Post by Mijikai »

RASHAD wrote:Hi
How about

Code: Select all

GetActiveGadget()
:shock:
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.
User avatar
Caronte3D
Addict
Addict
Posts: 1051
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: How to get focused gadget?

Post by Caronte3D »

@RASHAD, I think it's the function I need (I must read the entire functions of purebasic again :lol: )
@Mijikai, Your solution will be handy as well :wink:
Thanks both :D
BarryG
Addict
Addict
Posts: 3320
Joined: Thu Apr 18, 2019 8:17 am

Re: How to get focused gadget?

Post by BarryG »

Mijikai wrote:active and focus are differnet states
They are? What's the difference? They've always meant the same thing from what I've always read.

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()
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: How to get focused gadget?

Post by Mijikai »

BarryG wrote:
Mijikai wrote:active and focus are differnet states
They are? What's the difference? They've always meant the same thing from what I've always read.

Also, your GetHwndWithFocus() procedure can be replaced with a single API command: GetFocus_().
Its different, something can be active but not have the focus.
GetFocus_() will only work for the current thread.
User avatar
mk-soft
Always Here
Always Here
Posts: 5393
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: How to get focused gadget?

Post by mk-soft »

Without API for all OS :wink:

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
BarryG
Addict
Addict
Posts: 3320
Joined: Thu Apr 18, 2019 8:17 am

Re: How to get focused gadget?

Post by BarryG »

Mijikai wrote:something can be active but not have the focus.
Never heard of that before. Got an example?
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: How to get focused gadget?

Post by Mijikai »

BarryG wrote:
Mijikai wrote:something can be active but not have the focus.
Never heard of that before. Got an example?
Example:

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
BarryG
Addict
Addict
Posts: 3320
Joined: Thu Apr 18, 2019 8:17 am

Re: How to get focused gadget?

Post by BarryG »

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().
User avatar
mk-soft
Always Here
Always Here
Posts: 5393
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: How to get focused gadget?

Post by mk-soft »

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
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
BarryG
Addict
Addict
Posts: 3320
Joined: Thu Apr 18, 2019 8:17 am

Re: How to get focused gadget?

Post by BarryG »

mk-soft wrote:GetActiveWindow() and GetActiveGadget do not return the window handle hWnd, but the Purebasic internal PB-ID.
<Slaps forehead> Of course. I should've mentioned to use WindowID() and GadgetID() with them as well.

hwndActive = WindowID(GetActiveWindow())
hwndFocus = GadgetID(GetActiveGadget())

And before anyone says it: you should check that GetActive... is not -1 before using with WindowID()/GadgetID().
Post Reply