Anki Forums

How to assign a shortcut for a button of extra fields?

I am preparing a language deck which contains two buttons for extra information. One of them contains an example sentence and the other the translation of the example sentence.

So, I was wondering if there was any way to assign some shortcut keys for these buttons.

You can find the code of the buttons here:

<div id="buttonsBox">

<button onmouseover="showExs()">Exam</button>
<button onmouseover="showTra()">Trans</button>


</div>

<!------------------------------------------------> 


<script>

function showExs() {
  document.getElementById("showExs").style.display = "";
  document.getElementById("showTra").style.display = "none";
}

function showTra() {
  document.getElementById("showExs").style.display = "none";
  document.getElementById("showTra").style.display = "";
  
}


</script>

<div id="showExs" style="display:none">
{{Exs}}
{{Sentence audio}}
</div>
<!------------------------------------------------>
<div id="showTra" style="display:none">
{{Tra}}

Thank you in advance

Try adding the following code at the end of the <script> tag.

// Remove the event listener registered on the previous card
if (typeof myKeyboardEventHandler !== 'undefined') {
    document.removeEventListener('keydown', myKeyboardEventHandler);
}

function myKeyboardEventHandler(event) {
    if (event.key === 'F7') {
        showExs();
    } else if (event.key === 'F8') {
        showTra();
    }
}

document.addEventListener('keydown', myKeyboardEventHandler);

If you are using an older version of Anki 2.1, Refocus Card when Reviewing (2.1) add-on is required for shortcut keys to work. In addition, keys already assigned by Anki itself or add-ons cannot be used.

2 Likes

It works, thank you very much.

The problem is that you need to click on somewhere on the flashcard first. It is like when you have two different tabs in front of you and you cannot switch between them unless you click.

But I guess there is no way to fix it?

Updating to the latest version of Anki, or installing Refocus Card when Reviewing (2.1) addon (as i mentioned above) should probably fix the issue.

Updating from 2.1.35 to 2.1.38 solved the problem. Thank you.