How to notice change of focus? (solved)

Just starting out? Need help? Post your questions and find answers here.
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

How to notice change of focus? (solved)

Post by Little John »

Hi all,

I want my program to know, when the user changes the focus, e.g. by pressing the TAB key or by cklicking at a gadget with the mouse. In the docs, I found constants such as

Code: Select all

#PB_Event_Menu
#PB_Event_Gadget
and so on, but no constant for a change of the focus.
Now I've found out, that 49331 seems to work for that purpose, at least here on Windows XP.

Code: Select all

; Window Constants
Enumeration
   #WinMain
EndEnumeration

; Gadget Constants
Enumeration
   #ListIcon
   #BtnCheckAll
   #BtnSelect
EndEnumeration

Define Event, Count

If OpenWindow(#WinMain, 100, 100, 250, 170, "Focus test") = 0
   MessageRequester("Error", "OpenWindow() failed.")
   End
EndIf

ListIconGadget(#ListIcon, 5, 5, 240, 110, "", 30, #PB_ListIcon_CheckBoxes | #PB_ListIcon_FullRowSelect | #PB_ListIcon_AlwaysShowSelection)
AddGadgetColumn(#ListIcon, 1, "", 100)
AddGadgetColumn(#ListIcon, 2, "", 100)

ButtonGadget(#BtnCheckAll, 10, 130, 100, 25, "Button A")
ButtonGadget(#BtnSelect,  130, 130, 100, 25, "Button B")

AddGadgetItem(#ListIcon, -1, "" + #LF$ + "Peter" + #LF$ + "Smith")
AddGadgetItem(#ListIcon, -1, "" + #LF$ + "Mary"  + #LF$ + "Meyer")
AddGadgetItem(#ListIcon, -1, "" + #LF$ + "Jolly" + #LF$ + "Jumper")

SetGadgetState(#ListIcon, 0)
SetActiveGadget(#ListIcon)

Count = 0
Repeat 
   Event = WaitWindowEvent() 
   Select Event
      Case 49331
         Count + 1
         Debug "Focus change #" + Str(Count)
   EndSelect
Until Event = #PB_Event_CloseWindow
My current program doesn't have to be cross-platform, but it should run on several Windows versions. Do you think what I'm doing above is safe, or is there another solution?
I also tried using EventType() and GetActiveGadget(), but to no avail.

TIA, Little John
Last edited by Little John on Sat Oct 03, 2009 3:25 pm, edited 1 time in total.
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Re: Event number on change of focus?

Post by Fluid Byte »

Code: Select all

Enumeration
	#ListIcon
	#BtnCheckAll
	#BtnSelect
EndEnumeration

Define Event,Count

OpenWindow(0,100,100,250,170,"Focustest")
ListIconGadget(#ListIcon,5,5,240,110,"",30,#PB_ListIcon_CheckBoxes | #PB_ListIcon_FullRowSelect | #PB_ListIcon_AlwaysShowSelection)
AddGadgetColumn(#ListIcon,1,"",100)
AddGadgetColumn(#ListIcon,2,"",100)
ButtonGadget(#BtnCheckAll,10,130,100,25,"ButtonA")
ButtonGadget(#BtnSelect,130,130,100,25,"ButtonB")
AddGadgetItem(#ListIcon,-1,""+#LF$+"Peter"+#LF$+"Smith")
AddGadgetItem(#ListIcon,-1,""+#LF$+"Mary"+#LF$+"Meyer")
AddGadgetItem(#ListIcon,-1,""+#LF$+"Jolly"+#LF$+"Jumper")

SetGadgetState(#ListIcon,0)
SetActiveGadget(#ListIcon)

CurrentGadget = GetActiveGadget()

Repeat
	EventID = WaitWindowEvent()
	
	TempGadget = GetActiveGadget()
	
	If TempGadget ! CurrentGadget
		CurrentGadget = TempGadget
		Count + 1
		Debug"Focus change #" + Str(Count)
	EndIf	
Until EventID = #PB_Event_CloseWindow
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Event number on change of focus?

Post by Little John »

Oops. I have tried something similar, but obviously made a mistake. Vielen Dank, Fluid Byte. Thank you!

Regards, Little John
Post Reply