# How to fallback to default keystroke handlers for shortcuts?

**URL:** <https://forums.ankiweb.net/t/how-to-fallback-to-default-keystroke-handlers-for-shortcuts/23088>\
**Category:** Add-ons\
**Created:** [September 13, 2022, 8:46am UTC](https://forums.ankiweb.net/t/how-to-fallback-to-default-keystroke-handlers-for-shortcuts/23088 "2022-09-13T08:46:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vhong](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/vhong/32/10214_2.png) [@vhong](https://forums.ankiweb.net/u/vhong)\
**Post date:** [September 13, 2022, 8:46am UTC](https://forums.ankiweb.net/t/how-to-fallback-to-default-keystroke-handlers-for-shortcuts/23088/1 "2022-09-13T08:46:36Z")

</div>

Hi everyone,  
I’m working on an add-on that changes the behavior of scrolling keystrokes such as “Up” and “Down” for certain card types but not other. For example, for “IR3” cards, scroll 5% of the page when “Up” or “Down” button is pressed.

To achieve this, I’ve setup handlers for up/down shortcuts:

```python
        addHook('reviewStateShortcuts', self.setReviewShortcuts)

```

```python
    def setReviewShortcuts(self, shortcuts):
        shortcuts.extend([
            ('Down', self.lineDown),
            ('Up', self.lineUp)
        ])

```

However, I’m not sure how to fall back to the default scrolling behavior:

```python

    def lineUp(self):
        if isIrCard():
            currentPos = self._getCurrentPosition()
            movementSize = self.viewportHeight * self.settings['lineScrollFactor']
            newPos = max(0, (currentPos - movementSize))
            mw.web.eval('window.scrollTo(0, {});'.format(newPos))
        else:
            # Fall back to default scrolling behavior. How?

```

Any help is appreciated. Thank you in advance 🙂 .

---

<div class="post-metadata">

**Author:** ![abdo](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/abdo/32/654_2.png) [@abdo](https://forums.ankiweb.net/u/abdo)\
**Post date:** [September 13, 2022, 12:30pm UTC](https://forums.ankiweb.net/t/how-to-fallback-to-default-keystroke-handlers-for-shortcuts/23088/2 "2022-09-13T12:30:59Z")

</div>

Since the default behavior of the arrow keys is not defined in the reviewer, I think one way to go about this is to register a JavaScript keydown handler instead, and maintain some global JS flag to determine whether the current card is an IR card, calling `event.preventDefault()` only for IR cards.

## In Python side

```python

import json

from anki.cards import Card
from aqt import gui_hooks

def on_question_shown(card: Card) -> None:
    mw.reviewer.web.eval(f"var isIrCard = {json.dumps(isIrCard())};")

gui_hooks.reviewer_did_show_question.append(on_question_shown)
# TODO: Inject the JS code below and the scroll settings - see the webview_will_set_content docs
# gui_hooks.webview_will_set_content.append(on_webview_will_set_content)

```

## In JavaScript side

```javascript
document.addEventListener("keydown", (e) => {
  if (isIrCard) {
    if (e.code === "ArrowUp") {
      window.scrollTo(0, 100); // TODO: use the scroll settings
      e.preventDefault();
    }
    // TODO: Handle ArrowDown
  }
  // Do nothing for non-IR cards
});

```

_Not tested_

---

<div class="post-metadata">

**Author:** ![vhong](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/vhong/32/10214_2.png) [@vhong](https://forums.ankiweb.net/u/vhong)\
**Post date:** [September 14, 2022, 3:11am UTC](https://forums.ankiweb.net/t/how-to-fallback-to-default-keystroke-handlers-for-shortcuts/23088/3 "2022-09-14T03:11:16Z")

</div>

This looks promising. I’ll try it.  
Thank you!

---

<div class="post-metadata">

**Author:** ![vhong](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/vhong/32/10214_2.png) [@vhong](https://forums.ankiweb.net/u/vhong)\
**Post date:** [September 15, 2022, 3:41am UTC](https://forums.ankiweb.net/t/how-to-fallback-to-default-keystroke-handlers-for-shortcuts/23088/4 "2022-09-15T03:41:18Z")

</div>

@abdo thanks for the hint. It works! (with some tweaks for existing code)

Here’s a sample PR in case anyone faces a similar issue in the future: [Move movement keys handling to js by tvhong · Pull Request #4 · tvhong/incremental-reading · GitHub](https://github.com/tvhong/incremental-reading/pull/4)
