Shortcut for a hint button

I’m preparing a language deck which contains a hint button in the front card. It shows the first letter of the answer in case I don’t remember instantly.

So, I was wondering if there was any way to assign some shortcut keys for this button. (I’m also thinking how can I solve the problem of needing to click on somewhere on the flashcard first and then press the shortcut).

Can someone kindly help me? You can find the code of the buttons here:

Tks a lot!

<button class="button" type="button" onclick="

if

(document.getElementById('ID') .style.display=='none')

{document.getElementById('ID') .style.display=''}

else

{document.getElementById('ID') .style.display='none'}">

button_label

</button>

<div class="spoiler" id="ID" style="display:none">

{{hint_field}}

</div>

You can add a script like this to your template:

<script>
document.addEventListener('keyup', function (event) {
    if (event.key == "z" && event.altKey){ // <- customize your shortcut here
        document.querySelector("button").click()
    }
})
</script>

When choosing a shortcut, make sure it’s not used by anki somewhere else.

1 Like

Thank you so much for your time and help!

However, I’ve just tried out and couldn’t make it work. Could you please check if I’m doing something wrong? I’m really sorry to take up your time.

<script>
document.addEventListener('keyup', function (event) {
    if (event.key == "z" && event.altKey){ // ctrl + h
        document.querySelector("button").click()
    }
})
</script>

(my intention is to assign “ctrl” + “h” as the shorcut)

Ah sorry, i should have made this clearer. You have to replace it in code, the text after // is a comment.
For Ctrl + h you would have to do write this:

<script>
document.addEventListener('keyup', function (event) {
    if (event.key == "h" && event.ctrlKey){ // <- notice the values changing
        document.querySelector("button").click()
    }
})
</script>
1 Like

I really, really appreciate your patience <3 May I ask you one last question?

I could make it work perfectly typing only “h”. The code looks like this:

<script>
document.addEventListener('keyup', function (event) {
    if (event.key == "h"){ 
        document.querySelector("button").click()
    }
})
</script>

But, it didn’t work with Ctrl + h pasting precisely the code you sent me. I’m using a Mac, maybe would that be a problem?

Thanks again!

I couldn’t replicate the problem on my computer (I’m running linux), even when using webkit (same web engine as safari).
It’s possible that macos is the reason, maybe a different app is capturing the keyboard shortcut or the implementation of the web renderer used to display the flashcard is slightly different on mac, but someone with an apple computer would have to check it.

1 Like

No worries. Tried here Ctrl + a number instead of Ctrl + a letter and it worked perfectly!

Here’s an example of Ctrl + 9 assigned as a shortcut:

<script>
document.addEventListener('keyup', function (event) {
    if (event.ctrlKey && event.key == "9"){ 
        document.querySelector("button").click()
    }
})
</script>

Thank you very much for your help!

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