v18 How to set focus on a list control?
#1
Hello, 
I am new to making addons for kodi, and i have ran into some issues and i need some help.

I have a script addon that creates a custom dialog.  "using (xbmcgui.WindowXML)"
In my dialog xml file i have a button that has <ondown>5009</ondown>, and i have a list control with id 5009.

When the button has the focus and i press down, why does not the list control get the focus?
So, that pressing up & down changes item in the list.

xml:

<?xml version="1.0" encoding="UTF-8"?>
<window type="dialog" id=4999>
    <views>5001,5002,5003,5004,5005,5006,5007,5008,5009,5010,5011,5012,5013,5014,5015,5016,5017,5018</views>
    ...
    ...
    <controls>
        <include>CommonBackground</include>
        <onup></onup>
        <ondown></ondown>
        <onleft></onleft>
        <onright></onright>
        ...
        ...
        <control type="button" id=5005>
            ...
            ...
            <visible>true</visible>
            <onclick></onclick>
            <onup></onup>
            <ondown>5009</ondown>
            <onleft></onleft>
            <onright></onright>                
            ...
            ...
        </control>
        ...
        ...
        <control type="list" id=5009>
            ...
            ...
            <viewtype label="list">list</viewtype>
            <visible>true</visible>
            <pagecontrol>5010</pagecontrol>
            <onup></onup>
            <ondown></ondown>
            <onleft></onleft>
            <onright></onright>    
            <itemlayout>
                ...
                ...
            </itemlayout>
            <focusedlayout>
                ...
                ...
            </focusedlayout>
        </control>
        ...
        ...
    </controls>
</window>

i have tried in my script to force the focus on the list control using both setFocus and setFocusId, but none gives the list control focus.
What am i doing wrong and what is the solution to my problem ?
Reply
#2
Thread moved to addon development
|Banned add-ons (wiki)|Forum rules (wiki)|VPN policy (wiki)|First time user (wiki)|FAQs (wiki) Troubleshooting (wiki)|Add-ons (wiki)|Free content (wiki)|Debug Log (wiki)|

Kodi Blog Posts
Reply
#3
Try SetFocus(5009) instead.
Reply
#4
(2021-02-26, 16:21)Hitcher Wrote: Try SetFocus(5009) instead.

As i said. i already tried that, and it doesnt work with either of setFocus and setFocusid
I even tried using xbmc.executebuiltin('Control.SetFocus(5009, 0)')  still no go.
Reply
#5
Sorry, missed the bottom text.

Are you 100% sure the button itself gets focus in the first place because you don't appear to have set a default control for the window?
Reply
#6
(2021-02-26, 17:43)Hitcher Wrote: Sorry, missed the bottom text.

Are you 100% sure the button itself gets focus in the first place because you don't appear to have set a default control for the window?

Yes. 
As you see from my example xml. i got a lot of views and a lot of these are button controls.  i stripped my xml to make a minimum example of where the issue is.
I have checked in my script if controls gets focus by using function onFocus(self, onFocus)

The button never set the focus over to the list control  when down is pressed. 
If i move my mouse over the controls  buttons, scrollbar etc, i see the onFocus function is exectured.

However. if i move my mouse over the list control, onFocus is not executed.
But i can now use Up/Down to selects items in the list. 

And if i have set forexample a <onleft>5005</onleft> in the list control to go back to the button,
it will set focus back on to the button, so only way to get back to the list control is to use the mouse again.  Sad


python:

class MyGUI(xbmcgui.WindowXML):
    
     def __init__(self, *args, **kwargs):
        ...
        ...
        
    def onInit(self):
        ...
        ...


    def onFocus(self, controlId):
        
        if 5005 == controlId:
            print("button X has focus")
        
        if 5009 == controlId:
            print("list control has focus")

    
    def onClick(self, controlId):
        ...
    def onDoubleClick(self, controlId):
        ...    
    ...
    ...


if (__name__ == '__main__'):

    ui = MyGUI('test.xml', CWD, 'default', '1080i', True, optional1='some data')
    ui.doModal()
    del ui
Reply
#7
The <views></views> tag is only for lists not any other control.

You need to define a default control using <defaultcontrol always="true">5009</defaultcontrol> if it's the button you want to get initial focus.

Are you also sure there's no other control with that ID causing a conflict?
Reply
#8
(2021-02-26, 20:21)Hitcher Wrote: The <views></views> tag is only for lists not any other control.

You need to define a default control using <defaultcontrol always="true">5009</defaultcontrol> if it's the button you want to get initial focus.

Are you also sure there's no other control with that ID causing a conflict?

Ok. so i removed the other id's from the views.
<defaultcontrol always="true">5009</defaultcontrol> Does not seems to do anything. 

I have put together a tiny example where you can test this issue yourself.
Its nothing fancy, but it show the issue with ondown not setting focus to the list control.
It also shows that forcing it to the listcontrol dosent help either. 
I am woundering if there is some workaround to this or that if the controls needs to be placed under a group control.


C:\Users\USER\AppData\Roaming\Kodi\addons\script.testwindow1\addon.xml
xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<addon id="script.testwindow1" name="TestWindow" version="0.0.1" provider-name="me">
    <requires>
        <import addon="xbmc.python" version="2.25.0"/>
    </requires>
    <extension point="xbmc.python.script" library="default.py">
        <provides>executable</provides>
    </extension>
    <extension point="xbmc.addon.metadata">
        <summary lang="en_GB">window test script</summary>
        <description lang="en_GB">a test script to create a window</description>
        <language></language>
        <platform>all</platform>
    </extension>
</addon>

C:\Users\USER\AppData\Roaming\Kodi\addons\script.testwindow1\resources\skins\default\1080i\test.xml
xml:

<?xml version="1.0" encoding="UTF-8"?>
<window type="dialog" id=5000>
    
    <backgroundcolor>0x2d505a</backgroundcolor>
    <animation effect="fade" time="1200">WindowOpen</animation>
    <views>5003</views>
    
    <controls>
        <defaultcontrol always="true">5001</defaultcontrol>

        <control type="button" id=5001>
            <left>760</left>
            <top>45</top>
            <width>150</width>
            <height>50</height>
            <label>button 1</label>
            <font>font12</font>
            <align>center</align>
            <visible>true</visible>
            <onclick></onclick>
            <onup></onup>
            <ondown>SetFocus(5003)</ondown>
            <onleft></onleft>
            <onright>5002</onright>                
        </control>
        
        <control type="button" id=5002>
            <left>1110</left> 
            <top>45</top>
            <width>150</width>
            <height>50</height>
            <label>button 2</label>
            <font>font12</font>
            <align>center</align>
            <visible>true</visible>
            <onclick></onclick>
            <onup></onup>
            <ondown>5003</ondown>
            <onleft>5001</onleft>
            <onright></onright>            
        </control>        

        <control type="list" id=5003>
            <left>135</left>
            <top>120</top>
            <width>790</width>
            <height>770</height>
            <viewtype label="list">list</viewtype>
            <visible>true</visible>
            <scrolltime tween="sine" easing="out">200</scrolltime>        
            <onup></onup>
            <ondown></ondown>
            <onleft>5002</onleft>
            <onright>5001</onright>    
            
            <itemlayout height="80" width="620">
                <control type="label">
                    <left>144</left>
                    <top>0</top>
                    <height>75</height>
                    <width>620</width>
                    <font>font14</font>
                    <label>$INFO[ListItem.Label]</label>
                </control>
            </itemlayout>

            <focusedlayout height="80" width="620">
                <control type="label">
                    <left>144</left>
                    <top>0</top>
                    <height>75</height>
                    <width>620</width>
                    <font>font14</font>
                    <label>$INFO[ListItem.Label]</label>
                    <textcolor>orange</textcolor>
                </control>
            </focusedlayout>
        </control>
    </controls>
</window>

C:\Users\USER\AppData\Roaming\Kodi\addons\script.testwindow1\default.py
python:

import xbmc
import xbmcgui
import xbmcaddon

CID_WINDOW   = 5000
CID_BUTTON_1 = 5001
CID_BUTTON_2 = 5002
CID_LIST     = 5003

ADDON = xbmcaddon.Addon()
CWD = ADDON.getAddonInfo('path').decode('utf-8')


class MyGUI(xbmcgui.WindowXML):
    def __init__(self, *args, **kwargs):
        self.data = kwargs['optional1']

    def onInit(self):

        self.cbutton1 = self.getControl(CID_BUTTON_1)
        self.cbutton2 = self.getControl(CID_BUTTON_2)
        self.clist    = self.getControl(CID_LIST)

        # add some numbers to the list...
        listitems = []
        for i in range(0,25):
            item = xbmcgui.ListItem(str(i))
            listitems.append(item)
        self.clist.addItems(listitems)       
        
        self.setFocus(self.cbutton1)
        # self.setFocusId(CID_BUTTON_1)


    def onFocus(self, controlId):
        xbmc.executebuiltin('Notification(Focus on:'+str(controlId)+', ,1, "")')

    def onClick(self, controlId):
        
        if controlId == CID_BUTTON_1:
            xbmc.executebuiltin('Notification(Setting focus on list control, ,1, "")')
            self.setFocusId(CID_LIST)
       
        if controlId == CID_BUTTON_2:
            self.close()


if (__name__ == '__main__'):

    ui = MyGUI('test.xml', CWD, 'default', '1080i', True, optional1='some data')
    ui.doModal()
    del ui
Reply
#9
erm, in my test.xml  i have the <defaultcontrol always="true">5001</defaultcontrol> in the wrong place. it should be moved up a line.
but assigning the list control id  to it, does not set the list control in focus.
Reply
#10
An update!


After googeling, searching forum, wiki and so forth i found no answers to my issue. 
But while i was scrolling the wiki and looking at builtin functions, the Container.SetViewMode(id) caught my eye. 

So i tried putting that in my xml file in the <ondown> tag for my button. 

xml:

<control type="button" id="5001">
    ...
    <ondown>Container.SetViewMode(5003)</ondown>
</control>            
<control type="button" id="5002">
    ...
    <ondown>5003</ondown>
</control>    

Some progress. 
Button 1 did nothing on Down, but when i now moved the focus to button 2 and pressed down,  it set the focus to the listcontroll.  Yay...
Although this isnt the desired effect i wanted. atleast i was getting somewhere.

So next i modified my script a bit and got it working.


python:

class MyGUI(xbmcgui.WindowXML):
    
    def onInit(self):
        # variable to keep track of the control that has the focus.
        self.currentFocus = 5001

    def onFocus(self, controlId):
        # update the variable when changing focus
        self.currentFocus = controlId;        
        
    def onAction(self, action):
        # check if button1 or button2 pressed down.
        if ((self.currentFocus == 5001) or (self.currentFocus == 5002)) and (action.getId() == 4):
            
            # Need to set the focus first. idk why, but unelss its done focus is not changed.
            self.setFocusId(5003)
            xbmc.executebuiltin('Container.SetViewMode(5003)')



Hopefully this can be of help to others.
Reply
#11
Sounds like there's some conflict of IDs still if that works for one button but not the other. Open all XML files in an editor and search for those IDs.
Reply
#12
(2021-02-27, 10:14)Hitcher Wrote: Sounds like there's some conflict of IDs still if that works for one button but not the other. Open all XML files in an editor and search for those IDs.

So i did do a search in the xml files for the id's i have used.
And sure enough there was a couple of skins using the id's i had used.  
Although i dont use thouse skins, so it shouldnt be any conflict with the id.

Never the less i did change my ids starting from 5000... to 35000...  instead.
And re did my tests, but sadly no change.

I got the same behavioure as posted above.
So it seems like im stuck with this workaround i made. 

It would be helpfull if someone else tested this aswell, to make sure its not on my end something is off.
Reply

Logout Mark Read Team Forum Stats Members Help
How to set focus on a list control?0