From fe33677df45fd2eca7e8dfab84285cc1cc926184 Mon Sep 17 00:00:00 2001 From: Steefkuh <57499797+Steefkuh@users.noreply.github.com> Date: Sat, 14 Oct 2023 17:54:10 +0200 Subject: [PATCH 1/3] Make hotkey global --- pix2tex/gui.py | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/pix2tex/gui.py b/pix2tex/gui.py index 1b0e93b..42a4058 100644 --- a/pix2tex/gui.py +++ b/pix2tex/gui.py @@ -11,6 +11,7 @@ from PyQt6.QtWidgets import QMainWindow, QApplication, QMessageBox, QVBoxLayout, QWidget, \ QPushButton, QTextEdit, QFormLayout, QHBoxLayout, QDoubleSpinBox from pynput.mouse import Controller +from pynput.keyboard import Listener, HotKey from PIL import ImageGrab, Image import numpy as np @@ -21,6 +22,7 @@ import pix2tex.resources.resources class App(QMainWindow): isProcessing = False + hotkey_signal = pyqtSignal() def __init__(self, args=None): super().__init__() @@ -28,6 +30,7 @@ def __init__(self, args=None): self.model = cli.LatexOCR(self.args) self.initUI() self.snipWidget = SnipWidget(self) + self.hotkey_signal.connect(self.onClick) self.show() def initUI(self): @@ -63,8 +66,6 @@ def initUI(self): self.snipButton = QPushButton('Snip [Alt+S]', self) self.snipButton.clicked.connect(self.onClick) - self.shortcut = QtGui.QShortcut(QtGui.QKeySequence('Alt+S'), self) - self.shortcut.activated.connect(self.onClick) # Create retry button self.retryButton = QPushButton('Retry', self) @@ -86,6 +87,22 @@ def initUI(self): settings = QFormLayout() settings.addRow('Temperature:', self.tempField) lay.addLayout(settings) + + def hotkey(self): + def on_activate(): + self.hotkey_signal.emit() + + def for_canonical(f): + return lambda k: f(l.canonical(k)) + + hotkey = HotKey( + HotKey.parse('+s'), + on_activate) + l = Listener( + on_press=for_canonical(hotkey.press), + on_release=for_canonical(hotkey.release) + ) + l.start() def toggleProcessing(self, value=None): if value is None: @@ -102,7 +119,6 @@ def toggleProcessing(self, value=None): text = 'Snip [Alt+S]' func = self.onClick self.retryButton.setEnabled(True) - self.shortcut.setEnabled(not self.isProcessing) self.snipButton.setText(text) self.snipButton.clicked.disconnect() self.snipButton.clicked.connect(func) @@ -351,4 +367,5 @@ def main(arguments): os.environ['QTWEBENGINE_DISABLE_SANDBOX'] = '1' app = QApplication(sys.argv) ex = App(arguments) + ex.hotkey() sys.exit(app.exec()) From e33f88afec7be7939e0a689784ab7d0dfe9bac29 Mon Sep 17 00:00:00 2001 From: Steefkuh <57499797+Steefkuh@users.noreply.github.com> Date: Sat, 14 Oct 2023 18:53:45 +0200 Subject: [PATCH 2/3] Update gui.py --- pix2tex/gui.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pix2tex/gui.py b/pix2tex/gui.py index 42a4058..9946f6f 100644 --- a/pix2tex/gui.py +++ b/pix2tex/gui.py @@ -31,6 +31,7 @@ def __init__(self, args=None): self.initUI() self.snipWidget = SnipWidget(self) self.hotkey_signal.connect(self.onClick) + self.clipboard = QApplication.clipboard() self.show() def initUI(self): @@ -199,6 +200,8 @@ def returnPrediction(self, result): if success: self.displayPrediction(prediction) self.retryButton.setEnabled(True) + self.clipboard.setText(prediction) + else: self.webView.setHtml("") msg = QMessageBox() From cba68ca0957aae7d80b89e5f38a840238ed515a0 Mon Sep 17 00:00:00 2001 From: Steefkuh <57499797+Steefkuh@users.noreply.github.com> Date: Sat, 14 Oct 2023 18:55:06 +0200 Subject: [PATCH 3/3] Update gui.py --- pix2tex/gui.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pix2tex/gui.py b/pix2tex/gui.py index 9946f6f..2634c24 100644 --- a/pix2tex/gui.py +++ b/pix2tex/gui.py @@ -91,7 +91,8 @@ def initUI(self): def hotkey(self): def on_activate(): - self.hotkey_signal.emit() + if not self.isProcessing: + self.hotkey_signal.emit() def for_canonical(f): return lambda k: f(l.canonical(k))