Hello abdo,
If you can eventually just help me to finish this, it would be verry nice,
I can’t adapt this code of pyqtkeybind lib, maybe you know why it’s not working ?
So my purpose is to call a function addTextInFrontOnClickButton() with a shortcut, and execute it.
this is the tricky part at the end of my code:
window = QtWidgets.QMainWindow()
keybinder.init()
keybinder.register_hotkey(window.winId(), 'ctrl+l', addTextInFrontOnClickButton)
win_event_filter = WinEventFilter(keybinder)
event_dispatcher = QAbstractEventDispatcher.instance()
event_dispatcher.installNativeEventFilter(win_event_filter)
My total code is just below :
import sys
import time
# appending a path
sys.path.append("C:/Users/Admin/AppData/Roaming/Anki2/addons21/BtnCopyPaste")
import keyboard
import aqt
from aqt.editor import Editor
from ahk import AHK, Hotkey
from pyqtkeybind import keybinder
from PyQt5.QtWidgets import *
import DemoShortcut
VK_PACKET = False
from PyQt5.QtCore import QAbstractNativeEventFilter, QAbstractEventDispatcher
from PyQt5 import QtWidgets
class WinEventFilter(QAbstractNativeEventFilter):
def __init__(self, keybinder):
self.keybinder = keybinder
super().__init__()
def nativeEventFilter(self, eventType, message):
ret = self.keybinder.handler(eventType, message)
return ret, 0
def addTextInFrontOnClickButton(editor: Editor) -> None:
# action du bouton - pose le focus sur le logiciel word/ envoie les commandes ctrl+c / retourne dans anki sur le focus
# correspondant/ envoie les commandes ctrl+v avec pygui / retourne sur word.
(_, addcards) = aqt.dialogs._dialogs["AddCards"]
if addcards:
keyboard.send('ctrl+c')
field_index = addcards.editor.note.keys().index("Front")
addcards.editor.web.eval(f"focusField({field_index})")
keyboard.send('ctrl+v')
def addTextInBackOnClickButton(editor: Editor) -> None:
(_, addcards) = aqt.dialogs._dialogs["AddCards"]
if addcards:
keyboard.send('ctrl+c')
field_index = addcards.editor.note.keys().index("Back")
addcards.editor.web.eval(f"focusField({field_index})")
keyboard.send('ctrl+v')
def add_buttonInFront(buttons: list[str], editor: Editor):
buttons.append(
editor.addButton(
icon=None,
cmd="myaddon",
func=addTextInFrontOnClickButton,
label="FRT",
# Your shortcut
keys="Ctrl+S"
)
)
def add_buttonInBack(buttons: list[str], editor: Editor):
buttons.append(
editor.addButton(
icon=None,
cmd="myaddonn",
func=addTextInBackOnClickButton,
label="BCK",
# Your shortcut
keys="Ctrl+S"
)
)
aqt.gui_hooks.editor_did_init_buttons.append(add_buttonInBack)
aqt.gui_hooks.editor_did_init_buttons.append(add_buttonInFront)
window = QtWidgets.QMainWindow()
keybinder.init()
keybinder.register_hotkey(window.winId(), 'ctrl+l', addTextInFrontOnClickButton)
win_event_filter = WinEventFilter(keybinder)
event_dispatcher = QAbstractEventDispatcher.instance()
event_dispatcher.installNativeEventFilter(win_event_filter)
And i try to adapt it from the example from the sample of the library that you suggest to me.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Sample PyQt5 app to demonstrate keybinder capabilities."""
import sys
from PyQt5 import QtWidgets
from PyQt5.QtCore import QAbstractNativeEventFilter, QAbstractEventDispatcher
from pyqtkeybind import keybinder
class WinEventFilter(QAbstractNativeEventFilter):
def __init__(self, keybinder):
self.keybinder = keybinder
super().__init__()
def nativeEventFilter(self, eventType, message):
ret = self.keybinder.handler(eventType, message)
return ret, 0
def main():
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QMainWindow()
print("Sample app for pyqtkeybind:")
print("\tPress Ctrl+Shift+A or Print Screen any where to trigger a callback.")
print("\tCtrl+Shift+F unregisters and re-registers previous callback.")
print("\tCtrl+Shift+E exits the app.")
# Setup a global keyboard shortcut to print "Hello World" on pressing
# the shortcut
keybinder.init()
unregistered = False
def callback():
print("hello world")
def exit_app():
window.close()
def unregister():
keybinder.unregister_hotkey(window.winId(), "Shift+Ctrl+A")
print("unregister and register previous binding")
keybinder.register_hotkey(window.winId(), "Shift+Ctrl+A", callback)
keybinder.register_hotkey(window.winId(), "Shift+Ctrl+A", callback)
keybinder.register_hotkey(window.winId(), "Print Screen", callback)
keybinder.register_hotkey(window.winId(), "Shift+Ctrl+E", exit_app)
keybinder.register_hotkey(window.winId(), "Shift+Ctrl+F", unregister)
# Install a native event filter to receive events from the OS
win_event_filter = WinEventFilter(keybinder)
event_dispatcher = QAbstractEventDispatcher.instance()
event_dispatcher.installNativeEventFilter(win_event_filter)
app.exec_()
keybinder.unregister_hotkey(window.winId(), "Shift+Ctrl+A")
keybinder.unregister_hotkey(window.winId(), "Shift+Ctrl+F" )
keybinder.unregister_hotkey(window.winId(), "Shift+Ctrl+E")
keybinder.unregister_hotkey(window.winId(), "Print Screen")
if __name__ == '__main__':
main()
My problem is that i don’t know the anki architecture, and i’m not sure on how to proceed when i have to call the addwindow card.
Thanks for your help,