Stop playback with `[esc]`?

So, this is somewhat related to (it turns out: mine :smiley: ) previous topics:

and older:

The current problem I have is that because there is no sane solution for 1st topic (yet) and the 2nd is quite inconvenient - 5 key is close to 1-4 used to rate the answer, leading to quite often hitting the wrong key - I would like to suggest switching “Stop playback” from 5 to [esc] key:

  • it’s more separated = less chance to confuse it
  • [esc] is usually associated with cancelling some action, which would fit it better semantically.

Esc closes various dialogs, and I think it might get a bit confusing if it also served this purpose. I imagine this is a tweak you could make with an add-on for now however.

4 Likes

Well, indeed it closes some dialogues but! A lot depends on the context. If you are on the main card screen then there is no dialogue to close.

What’s more - if there is an entry field you can’t stop the playback with 5 as it’s being typed-in

Going back to this - as I was getting mildly infuriated by the inconvenient way to stop the playback often hitting 4 instead of 5 - I asked claud to conjure said addon… there was an issue in first version but the second one seems to work fine :smiley:

If anyone is interested:

  1. create subdirectory in Anki2/addons21 (eg. stop_audio_esc)
  2. create __init__.py file and paste in it following codee
# Stop Playback Remap Addon
# Remaps stop playback shortcut from '5' to 'Esc'

from aqt import gui_hooks
from aqt.reviewer import Reviewer
from aqt.qt import QKeySequence, Qt

def remap_stop_playback():
    """Remap the stop playback shortcut from '5' to 'Esc'"""
    
    # Store the original shortcut keys method
    original_shortcuts = Reviewer._shortcutKeys
    
    def modified_shortcut_keys(self):
        shortcuts = original_shortcuts(self)
        
        # Remove the '5' key binding for stop playback if it exists
        # shortcuts is a list of tuples: (key, function)
        shortcuts = [shortcut for shortcut in shortcuts if shortcut[0] != '5']
        
        # Add Esc key binding for stop playback
        # Find what function '5' was mapped to by checking the original method
        original_shortcuts_list = original_shortcuts(self)
        stop_playback_func = None
        
        for key, func in original_shortcuts_list:
            if key == '5':
                stop_playback_func = func
                break
        
        # If we found the stop playback function, map it to Esc
        if stop_playback_func:
            shortcuts.append(('Esc', stop_playback_func))
        else:
            # Fallback: directly use the stop playback method
            shortcuts.append(('Esc', lambda: self.mw.reviewer.on_stop_audio()))
        
        return shortcuts
    
    # Replace the shortcut keys method
    Reviewer._shortcutKeys = modified_shortcut_keys

# Initialize the remap when the addon loads
remap_stop_playback()

EDIT: well - it works even better than I expected as it allows stopping playback even if I’m in an input field - previously I had to hit [esc] either way and now it also stops the playback… AMAZING :smiley:
EDIT2: ok… there is a slight issue - it works as “play/pause” instead of stop (so the playback position is reset)… is there any API guide for Reviewer? Looking at Introduction - Writing Anki Add-ons I didn’t find anything like that…