How to map a key in the "Add" window that can be triggered when cursor is not on an input field?

I want to map a key in the window “Add” (which is opened when pressing a in the main menu) that removes all tags of the current card.

I am not used to writing addons yet, so I had to look to a few samples on the Internet. After some trial and error, I came up with this addon (see code block below). I saved the script in ~/.local/share/Anki2/addons21/my-addon/__init__.py

from aqt import gui_hooks, mw
from aqt.editor import Editor

def remove_all_tags(editor: Editor) -> None:
  editor.note.tags = []
  editor.loadNote()

def set_my_custom_shortcuts(cuts: list[tuple], editor: Editor) -> None:
  cuts.append(("Alt+Shift+R", lambda: remove_all_tags(editor)))

gui_hooks.editor_did_init_shortcuts.append(set_my_custom_shortcuts)

I can trigger the keybinding from within Anki and it is able to remove all tags when the cursor is on a text field (e.g. Front or Back). Ideally, the keybinding should work regardless whether the cursor is inside a text field or not.

My question is: How to bind a key in the “Add” window so that it can be triggered even when the cursor is outside a text field?

PS: I’m using Anki desktop version 24.06.2 in Ubuntu 22.04.4 LTS

1 Like

Try adding True as a third tuple element:

cuts.append(("Alt+Shift+R", lambda: remove_all_tags(editor), True))
3 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.