How do I make a key only work in a certain context?

This is the code I use to assign a key to a operator:

    sub_box = col.box()
    header_row(sub_box.row(align=True), 'show_posing', label='Posing')
    if preference.keymap.show_posing:
        sub_box = sub_box.box()
        sub_box.separator()
        keymap = key_config.keymaps['3D View']
        km_item = get_keymap_item(keymap, 'redthread.rotate_bone_hier', 'none', 'none')
        if km_item:
            sub_box.context_pointer_set("keymap", keymap)
            rna_keymap_ui.draw_kmi([], key_config, keymap, km_item, sub_box, 0)
        else:
            sub_box.label(text="Hotkey not found")

        sub_box.separator()
keymap_item = keymap.keymap_items.new("redthread.rotate_bone_hier", type='NONE', value='PRESS')
keys.append((keymap, keymap_item))

If I want this key to only be affecting pose mode, and not conflict with keys in other modes, what would I need to do?

You’re currently adding it to the 3D View keymap, which covers a lot of areas.

You have the option of adding it directly to the Pose keymap, which should eliminate a lot of the conflicts.

keymap = key_config.keymaps['Pose']

This still won’t 100% guarantee that there are no conflicts, but it should narrow down the possibility.

You can also add a poll function to the Operator to make sure that it can’t be called outside of where you want.

1 Like