Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Add example of custom global shortcut on Windows #3593

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/core/flameshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,46 @@
#include <QScreen>
#endif

#if defined(Q_OS_WIN)
#include <Windows.h>

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) {
// 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) {
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)
Expand Down Expand Up @@ -69,6 +109,9 @@ Flameshot::Flameshot()
qApp,
[this]() { history(); });
#endif
#if defined(Q_OS_WIN)
hook = SetWindowsHookEx(WH_KEYBOARD_LL, keyboardHook, NULL, 0);
#endif
}

Flameshot* Flameshot::instance()
Expand Down