Best practice for addon key bindings - own preferences or Blender's?

:duck:for (2): Using an IntProperty to store the KeyMapItem.id and then using keymap_items.from_id() looks like a potentially promising approach.

This post over on devtalk pointed me in the direction of doing a lookup, rather than storing a reference / pointer to the object directly:

(I think I may have seen something similar on BSE)

However, even if this approach works for creating a keymap and referencing it from the preferences panel, I’m not sure how to set the reference from the keymap operator back to the preset (the colour in this case)- the keymapinstance is created and the operator property is set using kmi.properties.colour (as in the example in the other thread linked), but this seems to be assigned by value, rather than by reference:

ie the kmi.properties.colour is set to [1,0,0,1] (the default value for the preset property .colour), and does not update when the preferences change.

Even if I point the preferences panel at kmi.properties.colour directly, it doesn’t work as expected- the colour associated with the key binding only updates when I add a new binding!

Sample code

For the addon preferences class:

 def draw(self, context):
        layout = self.layout

        for preset in self.presets:
            kmi = context.window_manager.\
                keyconfigs.addon.keymaps['Sequencer'].keymap_items.\
                from_id(preset.keymapitemid)
            row = layout.row()
            row.prop(preset, "colour_name")
            row.prop(kmi.properties, "colour")
            row.prop(kmi, "type", text="", full_event=True)
            row.operator(
                "preferences.keyitem_remove",
                text="",
                icon='X'
            ).item_id = preset.keymapitemid

        layout.operator("qte.newpreset", icon='ADD')

demo:

Description: I add a preset, change the colour (blue), hit the hotkey- the operator is called with the default value. I then change the key binding so not to conflict with the next preset and add another preset. Now pressing the hotkey changes to the colour I set (blue)! I set the colour to another (green), but pressing the key changes/keeps the text blue. Adding a third preset ‘updates’ the colour again and pressing the hotkey changes the colour to the updated one (green)

:confounded:

How should I get the operator to use the reference to the preferences (QTEPreferences.presets[item].colour), as opposed to a one off value? Or, is there a way to update the colour property set by the hotkey (kmi.properties.colour) when the colour changes in addon preferences? Or should I use a different approach altogether? :upside_down_face:

For clarity:

Lastly, as I am clearly having issues, I am still happy to hear thoughts on Q (1) if the approach is fundamentally misguided!