From 1f19a9e74d02e7fddf066cc47851c9407df30c2f Mon Sep 17 00:00:00 2001 From: Akash Mozumdar Date: Sat, 6 Nov 2021 16:52:26 -0600 Subject: [PATCH] use system global hotkey --- extensions/extrawindow.cpp | 28 ++++++++++++++++++---------- text.cpp | 2 +- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/extensions/extrawindow.cpp b/extensions/extrawindow.cpp index beac6e8..2ec15a6 100644 --- a/extensions/extrawindow.cpp +++ b/extensions/extrawindow.cpp @@ -13,6 +13,7 @@ #include #include #include +#include extern const char* EXTRA_WINDOW_INFO; extern const char* SENTENCE_TOO_BIG; @@ -36,6 +37,7 @@ extern const char* FONT; extern const char* SAVE_SETTINGS; constexpr auto DICTIONARY_SAVE_FILE = u8"SavedDictionary.txt"; +constexpr int CLICK_THROUGH_HOTKEY = 0xc0d0; QColor colorPrompt(QWidget* parent, QColor default, const QString& title, bool customOpacity = true) { @@ -153,7 +155,7 @@ private: }* outliner; }; -class ExtraWindow : public PrettyWindow +class ExtraWindow : public PrettyWindow, QAbstractNativeEventFilter { public: ExtraWindow() : PrettyWindow("Extra Window") @@ -168,7 +170,6 @@ public: { AUTO_RESIZE_WINDOW_HEIGHT, false, &ExtraWindow::SetAutoResizeHeight }, { SHOW_ORIGINAL, true, &ExtraWindow::SetShowOriginal }, { DICTIONARY, false, &ExtraWindow::SetUseDictionary }, - { CLICK_THROUGH, false, &ExtraWindow::SetClickThrough }, }) { // delay processing anything until Textractor has finished initializing @@ -176,22 +177,21 @@ public: auto action = menu.addAction(name, this, slot); action->setCheckable(true); action->setChecked(default); - if (slot == &ExtraWindow::SetClickThrough) - { - action->setShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_T)); - action->setShortcutContext(Qt::ApplicationShortcut); - addAction(action); - } } + + menu.addAction(CLICK_THROUGH, this, &ExtraWindow::ToggleClickThrough); + menu.addAction(MAX_SENTENCE_SIZE, this, [this] { settings.setValue(MAX_SENTENCE_SIZE, maxSentenceSize = QInputDialog::getInt(this, MAX_SENTENCE_SIZE, "", maxSentenceSize, 0, INT_MAX, 1, nullptr, Qt::WindowCloseButtonHint)); }); ui.display->installEventFilter(this); ui.display->setMouseTracking(true); + qApp->installNativeEventFilter(this); QMetaObject::invokeMethod(this, [this] { + RegisterHotKey((HWND)winId(), CLICK_THROUGH_HOTKEY, MOD_SHIFT | MOD_NOREPEAT, 0x58); show(); AddSentence(EXTRA_WINDOW_INFO); }, Qt::QueuedConnection); @@ -257,9 +257,9 @@ private: } } - void SetClickThrough(bool clickThrough) + void ToggleClickThrough() { - if (clickThrough) + if (clickThrough = !clickThrough) { setAttribute(Qt::WA_TransparentForMouseEvents, true); setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint); @@ -329,6 +329,14 @@ private: dictionaryWindow.move(ui.display->mapToGlobal(QPoint(x, y - dictionaryWindow.height()))); } + bool nativeEventFilter(const QByteArray&, void* message, LRESULT* result) override + { + auto msg = (MSG*)message; + if (msg->message != WM_HOTKEY || msg->wParam != CLICK_THROUGH_HOTKEY) return false; + ToggleClickThrough(); + return true; + } + bool eventFilter(QObject*, QEvent* event) override { if (useDictionary && event->type() == QEvent::MouseMove) ComputeDictionaryPosition(((QMouseEvent*)event)->localPos().toPoint()); diff --git a/text.cpp b/text.cpp index 5208b0e..531b65d 100644 --- a/text.cpp +++ b/text.cpp @@ -180,7 +180,7 @@ const char* SHOW_ORIGINAL_INFO = u8R"(Original text will not be shown Only works if this extension is used directly after a translation extension)"; const char* SIZE_LOCK = u8"Size lock"; const char* AUTO_RESIZE_WINDOW_HEIGHT = u8"Auto resize window height"; -const char* CLICK_THROUGH = u8"Click Through"; +const char* CLICK_THROUGH = u8"Click through"; const char* OPACITY = u8"Opacity"; const char* BG_COLOR = u8"Background color"; const char* TEXT_COLOR = u8"Text color";