How can I manage events for an addon
#1
Hi,
I'm developing an add-on and I want to manage the events of a remote.
I want to run a python function if, for example, I've pressed on the tab button.
Can you help me ?

Thank you.
Reply
#2
Look at keymap editor addon code that is on the official repository. Probably you're trying to achive something that has already been done.
Reply
#3
Hi,
It's not that. I want to assign a key to a function in MY addon.
Reply
#4
Which will lead to the same answer, look at keymap editor source code to understand how it is done. You need to create a new keymap: http://wiki.xbmc.org/index.php?title=keymap

Other option is to design a custom window, grab the action.getButtonCode() and link it directly to your function. This will only work as long as your addon (your window) is running. See below code (keymap editor by takoi) to have an idea.

Code:
from threading import Timer

pressed_key = KeyListener.record_key()
if pressed_key = 'foo': my_function()

class KeyListener(WindowXMLDialog):
    TIMEOUT = 5

    def __new__(cls):
        return super(KeyListener, cls).__new__(cls, "DialogKaiToast.xml", "")

    def __init__(self):
        self.key = None

    def onInit(self):
        self.getControl(401).addLabel('Press key')
        self.getControl(402).addLabel('Timeout in %.0f seconds..' % self.TIMEOUT)

    def onAction(self, action):
        code = action.getButtonCode()
        self.key = None if code == 0 else str(code)
        self.close()

    @staticmethod
    def record_key():
        dialog = KeyListener()
        timeout = Timer(KeyListener.TIMEOUT, dialog.close)
        timeout.start()
        dialog.doModal()
        timeout.cancel()
        key = dialog.key
        del dialog
        return key
Reply
#5
Hi,
The second proposition is the good but I don't know how can I listen the keyboard as long as my add-on is running.
Thank you.
Reply
#6
(2014-10-01, 14:00)LitchiTheCat Wrote: Hi,
The second proposition is the good but I don't know how can I listen the keyboard as long as my add-on is running.
Thank you.

xbmcgui.Window* classes (both Pythonic and XML-based) have doModal() method which starts the event loop for your addon. After entering the event loop you can catch key actions via onAction() method like in the example above.
Just to be clear: XBMC catches not keys per se but key actions, and key action are mapped to keyboard keys via keyboard.xml.
Reply
#7
Hi,
Can you give me an example, please ?
Thanks a lot.
Reply
#8
(2014-10-02, 17:08)LitchiTheCat Wrote: Hi,
Can you give me an example, please ?
Thanks a lot.

The example is already here. That addon is using the "dialogkaitoast.xml" you need to create a costum window for yourself or something "invisible" to be able to catch the actions.
No one will make the addon for you, I think the info on this thread is enough for you to start.
Reply
#9
Hi,
I have a first version, it's this:
class KeyboardListener(WindowDialog):



def __init__(self):
self.key = None
WindowDialog.doModal(self)
self.urlChaines = urlChaines


def onInit(self):
"""initialisation"""
def onAction(self,action):
code = action.getButtonCode()
self.key = None if code == 0 else str(code)
self.key = str(self.key)

#conditions
self.close()


But the problem is: when I run the addon, I can catch the key actions, I can launch my function but I can't use the buttons on my addon.

Thanks
Reply
#10
Hi,
Can you help me please ?
How can I use this model while being able to move the cursor and click on the buttons ?

Please, help me !...

Thank you.
LitchiTheCat
Reply
#11
Up !
Reply
#12
Hi,
is there anybody have got the file "xbmcgui.py" ?
Reply
#13
(2014-11-25, 18:40)LitchiTheCat Wrote: Hi,
is there anybody have got the file "xbmcgui.py" ?

It does not exist.
Reply

Logout Mark Read Team Forum Stats Members Help
How can I manage events for an addon0