From 5956b18420e441c6038b95349f39739d9c6d583a Mon Sep 17 00:00:00 2001 From: dsatyanov Date: Sat, 4 May 2024 13:55:09 +0500 Subject: [PATCH 1/2] Add example of custom global shortcut on Windows --- src/core/flameshot.cpp | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/core/flameshot.cpp b/src/core/flameshot.cpp index c7eadcadb6..10c2981aa8 100644 --- a/src/core/flameshot.cpp +++ b/src/core/flameshot.cpp @@ -37,6 +37,41 @@ #include #endif +#if defined(Q_OS_WIN) +#include + +HHOOK hook; +bool isAltPressed = false; +bool isShiftPressed = false; + +// Just an example of global shortcuts on Windows. +// This is hardcoded Alt+Shift+3 shortcut +LRESULT CALLBACK keyboardHook(int nCode, WPARAM wParam, LPARAM lParam) { + if (nCode >= 0) { + KBDLLHOOKSTRUCT *kbdStruct = (KBDLLHOOKSTRUCT *)lParam; + if (wParam == WM_SYSKEYDOWN) { + if (kbdStruct->vkCode == VK_LMENU) { + isAltPressed = true; + } else if (isAltPressed && isShiftPressed && kbdStruct->vkCode == '3') { + Flameshot::instance()->gui(); + return 1; + } + } else if (wParam == WM_KEYDOWN) { + if (kbdStruct->vkCode == VK_LSHIFT) { + isShiftPressed = true; + } + } else if (wParam == WM_KEYUP) { + if (kbdStruct->vkCode == VK_LMENU) { + isAltPressed = false; + } else if (kbdStruct->vkCode == VK_LSHIFT) { + isShiftPressed = false; + } + } + } + return CallNextHookEx(hook, nCode, wParam, lParam); +} +#endif + Flameshot::Flameshot() : m_captureWindow(nullptr) , m_haveExternalWidget(false) @@ -69,6 +104,9 @@ Flameshot::Flameshot() qApp, [this]() { history(); }); #endif +#if defined(Q_OS_WIN) + hook = SetWindowsHookEx(WH_KEYBOARD_LL, keyboardHook, NULL, 0); +#endif } Flameshot* Flameshot::instance() From 75323ab3c86e0094b3fee59c39d79940ac02b6c5 Mon Sep 17 00:00:00 2001 From: dsatyanov Date: Sat, 4 May 2024 14:42:54 +0500 Subject: [PATCH 2/2] Support both Alt+Shift and Shift+Alt order combinations --- src/core/flameshot.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/core/flameshot.cpp b/src/core/flameshot.cpp index 10c2981aa8..03bccab47d 100644 --- a/src/core/flameshot.cpp +++ b/src/core/flameshot.cpp @@ -51,13 +51,18 @@ LRESULT CALLBACK keyboardHook(int nCode, WPARAM wParam, LPARAM lParam) { KBDLLHOOKSTRUCT *kbdStruct = (KBDLLHOOKSTRUCT *)lParam; if (wParam == WM_SYSKEYDOWN) { if (kbdStruct->vkCode == VK_LMENU) { + // We're here no matter of order Alt and Shift isAltPressed = true; + } else if (kbdStruct->vkCode == VK_LSHIFT) { + // We're here if Alt was pressed BEFORE Shift + isShiftPressed = true; } else if (isAltPressed && isShiftPressed && kbdStruct->vkCode == '3') { Flameshot::instance()->gui(); return 1; } } else if (wParam == WM_KEYDOWN) { if (kbdStruct->vkCode == VK_LSHIFT) { + // We're here if Shift was pressed BEFORE Alt isShiftPressed = true; } } else if (wParam == WM_KEYUP) {