hasfocus() bug?

I’m creating a search component and as a user moves off the miniKeyboard, I want the focus to move to a Submit button. I’m finding that some widgets are not responding to “hasFocus()” even though setFocus(true) works fine. It’s important to note this function doesn’t come back as invalid…

Here’s the code:

Function onKeyEvent(key as String, press as Boolean) as Boolean
    if press then
        print "KEY: "; key
        ' Just to remove any question that we have focus!
        m.miniKeyboard.setFocus(true)
        print "KEYBOARD FOCUS: "; m.miniKeyboard.hasFocus()
        if (key = "down") and (m.miniKeyboard.hasFocus() = true)
            m.searchBtn.setFocus(true)
            return true
        end if
    end if

    return false
End Function

Output after pushing “down” from the last button in the miniKeyboard.

KEY: down
KEYBOARD FOCUS: false
KEY: down
KEYBOARD FOCUS: false

Any idea?
Thanks

Your code will fail if m.searchBtn is out of scope or is being seen as the wrong type.
You can test this by placing a STOP after print "KEYBOARD FOCUS: "; m.miniKeyboard.hasFocus() and then do a print m.searchBtn in the debugger to see what it thinks it is.

What you’re trying to do does work if the search button is appended as a child to the minikeyboard - it makes it a bit simpler at least in my experience, besides, you probably want to reset the position to “a” each time the search window is initiated instead of keeping the selection on the last item highlighted - consider that a bug with the keyboards that they need to be destroyed and recreated to fix that issue.

search screen xml

		<component name="SearchScreen" extends="Group">
			<children>
				<Rectangle/>
				<Button id="Search"/>
				<MiniKeyboard id="MK"/>
			</children>
		</component>

xml addition for minikeyboard to screen you want to add it to

		<!-- Search screen -->
		<SearchScreen
			id="MK"
			visible="FALSE"/>

Init brs for the screen you want to add it to

		m.SearchScreen=m.top.findNode("MK")

OnItemSelected brs

		'Destroy and Recreate Search Screen to reset focus to "a"
		m.searchscreen.removeChildIndex(2) 'destroy minikeyboard
		m.searchscreen.removeChildIndex(1) 'destroy button
		m.searchscreen.removeChildIndex(0) 'destroy rectangle
		m.searchscreen.createChild("Rectangle")
		m.searchscreen.createChild("Button")
		m.searchscreen.createChild("MiniKeyboard")
		
		m.r1=m.searchscreen.getChild(0)
		m.r1.width=558'228
		m.r1.height=759'76
		m.r1.color="0x0F0F0FFF"
		m.r1.translation=[14,0]
		
		m.b1=m.searchscreen.getChild(1)
		m.b1.id="search"
		m.b1.text="Search"
		m.b1.showFocusFootprint=FALSE
		m.b1.minWidth=240
		m.b1.maxWidth=1000
		m.b1.translation=[164,670]
		m.b1.observeField("buttonSelected","SearchActivated")
		
		m.searchscreen.translation=[668,170]
		If m.devicemodel="4200X"
			m.SearchScreen.GetChild(2).keyboardBitmapUri="pkg:/images/search.png"
		Else
			m.SearchScreen.GetChild(2).keyboardBitmapUri="pkg:/images/searchfhd.png"
		End If
		
		m.searchscreen.GetChild(2).texteditbox.maxTextLength=20
		m.searchscreen.GetChild(2).texteditbox.backgroundUri="pkg:/images/searchtext.png"
		m.searchscreen.getchild(2).text=""
		m.searchscreen.visible=TRUE

OnKeyEvent brs

		If m.searchscreen.visible=TRUE 'Handle search screen keys
			If key="up"
				If m.searchscreen.getchild(1).hasFocus()
					m.searchscreen.getchild(2).SetFocus(TRUE)
				End If
			Else If key="down"
				If Not m.searchscreen.getchild(1).hasFocus()
					m.searchscreen.getchild(1).SetFocus(TRUE)
				End If
			End If
		End If

SearchActivated callback - for when the button is selected from the search screen

Function SearchActivated()

	temp=m.searchscreen.getchild(2).Text
m.SearchScreen.visible=FALSE ' make search screen disappear from display

       'do what you want to do with the search term and display results or no results
'remember to setfocus the new screen with the results or no results
End Function

Note in the code above, the Search Button has a higher z-order in creation than the mini keyboard.
First it draws a black rectangle background for the text entry box
Second it draws the Search Button
Third it draws the mini Keyboard on top of both of those

It could be your code is correct but the order is wrong, or you are seeing a race condition where it is taking longer to set the focus between the two objects in your code, or it could simply be a bug in the firmware, but go ahead and try making some changes to see if it resolves it for you.

Thanks for the help. I was able to get it working by the following, below. It appears if the button is a child of the miniKeyboard, which it was, it’s tracked by the “focusedChild” item.


    if press then
        'm.searchBtn.setFocus(true)
        if (key = "down") and (m.miniKeyboard.focusedChild.id = "")
            m.searchBtn.setFocus(true)
            return true
        else if (key = "up") and (m.miniKeyboard.focusedChild.id = "searchBtn")
            m.miniKeyboard.setFocus(true)
            return true
        end if
    end if

Cool. :slightly_smiling_face:
You’re welcome. Glad you fixed it.