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

**URL:** <https://blenderartists.org/t/best-practice-for-addon-key-bindings-own-preferences-or-blenders/1416828>\
**Category:** Python Support\
**Created:** [November 14, 2022, 1:58pm UTC](https://blenderartists.org/t/best-practice-for-addon-key-bindings-own-preferences-or-blenders/1416828 "2022-11-14T13:58:44Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![bertieb](https://blenderartists.org/user_avatar/blenderartists.org/bertieb/32/654946_2.png) [@bertieb](https://blenderartists.org/u/bertieb)\
**Post date:** [November 14, 2022, 2:42pm UTC](https://blenderartists.org/t/best-practice-for-addon-key-bindings-own-preferences-or-blenders/1416828/2 "2022-11-14T14:42:36Z")

</div>

> [@bertieb](#):
>
> (2) How can I let the user define presets that tie together a user-defined property (eg colour, location, size, duration) with a user-defined key binding?

🦆for (2): Using an `IntProperty` to store the [`KeyMapItem.id`](https://docs.blender.org/api/current/bpy.types.KeyMapItem.html#bpy.types.KeyMapItem.id) and then using [`keymap_items.from_id()`](https://docs.blender.org/api/current/bpy.types.KeyMapItems.html#bpy.types.KeyMapItems.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:

> **[Getting a PointerProperty to work](https://devtalk.blender.org/t/getting-a-pointerproperty-to-work/10878/2)**
>
> If I remember correctly, PointerProperties can’t be used in operators. You could identify the light by name instead (use a StringProperty).

(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](https://blenderartists.org/t/passing-an-argument-to-an-operator/1415928/16)), 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:
> 
> ```python
> 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)_

😖

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? 🙃

For clarity:

 ![blender-addon-kmi-noupdate3b](https://blenderartists.org/uploads/default/original/4X/e/0/6/e06c9378c7f339134a1c78d65695207b866c3b13.jpeg)

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

---

_[View the full topic](https://blenderartists.org/t/best-practice-for-addon-key-bindings-own-preferences-or-blenders/1416828)._
