- How to get search result when typing without hitting enter button in Anki Browse?

Hy

  • I would like for my cursor to automatically go to the search result after I stop typing. For example, I type “thing”, after 100ms (or whatever the debounce/delay is), I’d like the cursor to automatically go to the first match (if it exists) rather than me having to constantly hit ‘enter’, which gets a bit sore having to press ‘enter’ 100 times per day
  • I mean I want to show my search result in Anki browser when user start typing in search input field.
  • Anybody can help? I’ll appreciate it.Thanks :heart:
  • I’ve searched and tried a lot and am still looking for a Python script or Anki Add-ons that makes me show the search result instantly when typing without hitting enter button or search button in Anki browse. For ex, I type “thing”, after 100ms (or whatever the debounce/delay is), I’d like to automatically show the search result (if it exists) rather than me having to constantly hit ‘enter’, which gets a bit sore having to press ‘enter’ 100 times per day.

  • I mean I want to show my search result in Anki browser instantly when I start typing in search input field without press enter button or search button in Anki browse.

  • I’ve got this Python script, it’s almost about to solve the problem :

  1. Create a new folder named incremental_searchor something in the addons21folder in Anki’s data folder 1.
  2. Create a new file named init.pyin that folder, and enter the following code:
from typing import cast

from aqt.browser import Browser
from aqt.gui_hooks import browser_will_show
from aqt.qt import QLineEdit, QTimer


def on_browser_will_show(browser: Browser) -> None:
    def on_text_changed() -> None:
        if line_edit.text():
            timer.start(300)
        else:
            timer.stop()

    def on_timeout() -> None:
        browser.onSearchActivated()

    timer = QTimer(browser)
    timer.timeout.connect(on_timeout)
    line_edit = cast(QLineEdit, browser.form.searchEdit.lineEdit())
    line_edit.textChanged.connect(on_text_changed)


browser_will_show.append(on_browser_will_show)

https://forums.ankiweb.net/t/how-to-get-search-result-instantly-when-typing-without-hitting-enter-button-or-search-button-in-anki-browse-using-ahk-or-anki-add-ons/30167

  • I’ve searched and tried a lot and am still looking for an AHK script or Anki Add-ons that makes the cursor to automatically go to the search instantly when typing without hitting enter button or search button in Anki browse. For ex, I type “thing”, after 100ms (or whatever the debounce/delay is), I’d like the cursor to automatically go to the first match (if it exists) rather than me having to constantly hit ‘enter’, which gets a bit sore having to press ‘enter’ 100 times per day.
  • I mean I want to show my search result in Anki browser instantly when user start typing in search input field without press enter button or search button in Anki browse.
  • I’ve got this script, it’s almost about to solve the problem.
  • this script (send, enter) after each character I enter in any input location in the browse, So if I type in the field designated to stock something (front, back), it does a line break after each character I enter.
  • could someone makes this process (send, enter) “after delay” works only in the search field knowing that its coordinates for the browse window are X431, Y76.
  • Please help! I appreciate your time and effort. Thanks in advance.
  • This is the picture of Anki Browse :

  • this is the Autohotkey script:
; ======================================

timeout   := 600 ; Wait (ms) before sending ENTER
winTitle  := "Browse ahk_exe anki.exe"
Global ih := InputHook("V")
ih.OnChar := Func("go").Bind(timeout)
Loop {
 WinWaitActive % winTitle
 ih.Start()
 WinWaitNotActive
 SetTimer reset, Off
 ih.Stop()
}
go(wait, ih, char) {  ; Called when a character is added to the input buffer
 SetTimer reset, % -wait
}
reset() {
 ih.Stop()
 If ih.Input != "" {
  Send {Enter}
 }
 ih.Start()
}
; ======================================

I’ve created a small add-on. Although I haven’t tested it much, it seems to be working, so please feel free to give it a try.

  1. Create a new folder named incremental_search or something in the addons21 folder in Anki’s data folder.
  2. Create a new file named __init__.py in that folder, and enter the following code:
from typing import cast

from aqt.browser import Browser
from aqt.gui_hooks import browser_will_show
from aqt.qt import QLineEdit, QTimer


def on_browser_will_show(browser: Browser) -> None:
    def on_text_changed() -> None:
        if line_edit.text():
            timer.start(300)
        else:
            timer.stop()

    def on_timeout() -> None:
        browser.onSearchActivated()

    timer = QTimer(browser)
    timer.timeout.connect(on_timeout)
    line_edit = cast(QLineEdit, browser.form.searchEdit.lineEdit())
    line_edit.textChanged.connect(on_text_changed)


browser_will_show.append(on_browser_will_show)

You can change 300 to your liking.

3 Likes
  • You almost did it.
  • It works, When I type in the search field it does
    what I want, But it has some problems :
    1- When I’m typing in the search field and I want
    to select all the words in it or (with 2
    clicks or Ctrl+A) or move the cursor back, The
    cursor automatically and quickly returns to the
    front after doing that.
    2 - The same problem (1) when I type in the field
    designed to stock or edit somthing in the
    browse (Front,Back).
    3 - when I scroll down by Mouse wheel, the scroll bar returns to the place where it was the first time.
    4 - Sometimes Anki stops working (I think that’s because of the previous problems).
  • Could you please try it yourself?
  • Thanks a lot !
1 Like

@yfjuu please do not create multiple posts about the same topic.

Thanks for the feedback and sorry for the late reply. I’ve been a little busy the past few days and haven’t had time to look into the issues. I have fixed them now, so give it a try.

from typing import cast

from aqt.browser import Browser
from aqt.gui_hooks import browser_will_show
from aqt.qt import QLineEdit, QTimer


def on_browser_will_show(browser: Browser) -> None:
    def on_text_changed(text: str) -> None:
        if text:
            timer.start(300)
        else:
            timer.stop()

    def on_timeout() -> None:
        browser.onSearchActivated()

    timer = QTimer(browser)
    timer.setSingleShot(True)
    timer.timeout.connect(on_timeout)
    line_edit = cast(QLineEdit, browser.form.searchEdit.lineEdit())
    line_edit.textEdited.connect(on_text_changed)


browser_will_show.append(on_browser_will_show)

By the way, whether or not this add-on will work comfortably depends on the specs of your computer and the number of notes in your collection. I tested it with a collection of 10,000 notes on a high spec machine and it worked comfortably, but when I tested it with 300,000 notes on a low spec machine, it froze a bit every time I typed a single character, so it was not practical at all.

4 Likes

Thank you very much.
Now, The process had become work almost without any problem, But there is still only one problem :
For example, when I want to search for a word (“something”) and type the word in the search field and I misspelled some letters of the word (inside or at the beginning of the word), or I forgot to write some letters (inside or at the beginning of the word), or if I want to delete some letters from it (inside or at the beginning of the word) for ex: (“Sonting”) , (“omting”).
When I move the caret to where I want to replace or add or delete some characters (inside or at the beginning of the word), as soon as I type just one character or delete one, the caret automatically and quickly returns to the end of the word after I type or delete just one.

  • Can you make the caret not return automatically to the end of the word after typing or deleting inside or at the beginning of the word.

Hy how are you?
Did you find any solution for this last problem?

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