# How to use gui\_hooks reviewer\_will\_ show\_context\_menu?

**URL:** <https://forums.ankiweb.net/t/how-to-use-gui-hooks-reviewer-will-show-context-menu/21423>\
**Category:** Add-ons\
**Created:** [July 13, 2022, 8:59am UTC](https://forums.ankiweb.net/t/how-to-use-gui-hooks-reviewer-will-show-context-menu/21423 "2022-07-13T08:59:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ijgnd](https://avatars.discourse-cdn.com/v4/letter/i/bbe5ce/32.png) [@ijgnd](https://forums.ankiweb.net/u/ijgnd)\
**Post date:** [July 13, 2022, 8:59am UTC](https://forums.ankiweb.net/t/how-to-use-gui-hooks-reviewer-will-show-context-menu/21423/1 "2022-07-13T08:59:40Z")

</div>

[in the title I had to split up the name of the hook because the forum software won’t allow me to post long words in the title]

This hook does not work for me.

I have tried in windows and linux, with 2.1.49/qt5 and the latest dev version with qt 6.3.1.

I have this minimal add-on:

```python
from aqt import mw
from aqt import gui_hooks
from aqt.utils import showText

print("... in general print messages are visible")

def helper(reviewer):
    showText("test")
 
def add_entry(reviewer, menu):
    print("...hook was called")
    
    # the next two lines are from add-on "Show Question"
    # https://ankiweb.net/shared/info/1566569834
    entry = [["Show Question", "", reviewer._showQuestion]]
    reviewer._addMenuItems(menu, entry)

    menutext = "show info box"
    action_nid = menu.addAction(menutext)
    qconnect(action_nid.triggered, lambda _, r=reviewer:helper(r))

gui_hooks.reviewer_will_show_context_menu.append(add_entry)

```

I have made [this screencast](https://raw.githubusercontent.com/ijgnd/temp/main/gui_hooks ___reviewer_will_show_context_menu__ problem.mp4) to show what I’ve done and what I expect. In this screencast I use just one add-on - a stripped down version of [Show Question - AnkiWeb](https://ankiweb.net/shared/info/1566569834) (which didn’t work for me). This screencast was made in linux with the latest anki dev version and qt 6.3.1 and python 3.9.10.

with the add-on provided I expect to see the message printed “…hook was called” when I right click in the reviewer. This does not work. I just get the default “copy” in the context menu.

---

<div class="post-metadata">

**Author:** ![glutanimate](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/glutanimate/32/97_2.png) [@glutanimate](https://forums.ankiweb.net/u/glutanimate)\
**Post date:** [July 13, 2022, 9:27am UTC](https://forums.ankiweb.net/t/how-to-use-gui-hooks-reviewer-will-show-context-menu/21423/2 "2022-07-13T09:27:52Z")

</div>

The naming of this hook is a bit unfortunate, got confused by it my own share of times. It doesn’t modify the right-click context menu, but the menu you see when clicking on the “More” button in the reviewer bottom bar.

To modify the reviewer right-click context menu you’ll have to use `webview_will_show_context_menu`, and then assert that the reviewer is active through some heuristics like:

```python
def on_webview_will_show_context_menu(webview: "AnkiWebView", menu: QMenu):
    # shaky heuristic to determine which web view we are in
    if hasattr(webview, "title"):
        if webview.title != "main webview":
            return

    if mw.state != "review" or not webview.selectedText():
        return

```

---

<div class="post-metadata">

**Author:** ![ijgnd](https://avatars.discourse-cdn.com/v4/letter/i/bbe5ce/32.png) [@ijgnd](https://forums.ankiweb.net/u/ijgnd)\
**Post date:** [July 13, 2022, 10:26am UTC](https://forums.ankiweb.net/t/how-to-use-gui-hooks-reviewer-will-show-context-menu/21423/3 "2022-07-13T10:26:22Z")

</div>

thanks. Indeed it’s obvious once you check what calls the function showContextMenu …
