mirror of
https://github.com/Skycoder42/QHotkey.git
synced 2026-11-19 12:49:58 +03:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
34330d6ff5 | ||
|
|
6a0cc377e6 | ||
|
|
fcf83f3e36 | ||
|
|
64fec66d35 | ||
|
|
1114df5081 | ||
|
|
e0bc2a9b9f | ||
|
|
1d49a63827 | ||
|
|
7f5840fd34 | ||
|
|
7b10ea6d64 | ||
|
|
79e5dd65be | ||
|
|
dbec7d3a2d | ||
|
|
6b3cc35bbd | ||
|
|
25f21849b3 | ||
|
|
eb7ddaba57 | ||
|
|
8584ff1b15 | ||
|
|
384f780b28 | ||
|
|
4bb901a41e | ||
|
|
2a87965ed6 | ||
|
|
f0799fd4a4 | ||
|
|
53745ba936 | ||
|
|
b73b4955ae | ||
|
|
bd35d8b07a | ||
|
|
941cb2f15f | ||
|
|
4f76792098 | ||
|
|
483e07365e |
@@ -0,0 +1,71 @@
|
|||||||
|
name: Build
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches-ignore:
|
||||||
|
- "releases/**"
|
||||||
|
paths-ignore:
|
||||||
|
- "**.md"
|
||||||
|
pull_request:
|
||||||
|
paths-ignore:
|
||||||
|
- "**.md"
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
qt_version: [5.12.11, 5.15.2, 6.2.0]
|
||||||
|
platform: [ubuntu-20.04, windows-latest, macos-latest]
|
||||||
|
include:
|
||||||
|
- qt_version: 6.2.0
|
||||||
|
additional_arguments: -D QT_DEFAULT_MAJOR_VERSION=6
|
||||||
|
- platform: ubuntu-20.04
|
||||||
|
make: make
|
||||||
|
CXXFLAGS: -Wall -Wextra -pedantic -Werror
|
||||||
|
MAKEFLAGS: -j2
|
||||||
|
- platform: macos-latest
|
||||||
|
make: make
|
||||||
|
CXXFLAGS: -Wall -Wextra -pedantic -Werror -Wno-gnu-zero-variadic-macro-arguments # Ignore false-positive warning for qCWarning
|
||||||
|
MAKEFLAGS: -j3
|
||||||
|
- platform: windows-latest
|
||||||
|
make: nmake
|
||||||
|
CXXFLAGS: /W4 /WX /MP
|
||||||
|
|
||||||
|
runs-on: ${{ matrix.platform }}
|
||||||
|
env:
|
||||||
|
CXXFLAGS: ${{ matrix.CXXFLAGS }}
|
||||||
|
MAKEFLAGS: ${{ matrix.MAKEFLAGS }}
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Clone repo
|
||||||
|
uses: actions/checkout@v2.3.4
|
||||||
|
|
||||||
|
- name: Install Qt
|
||||||
|
uses: jurplel/install-qt-action@v2.14.0
|
||||||
|
with:
|
||||||
|
version: ${{ matrix.qt_version }}
|
||||||
|
|
||||||
|
- name: Build with CMake as static
|
||||||
|
run: |
|
||||||
|
cmake . -D QHOTKEY_EXAMPLES=ON ${{ matrix.additional_arguments }}
|
||||||
|
cmake --build .
|
||||||
|
|
||||||
|
- name: Build with CMake as shared
|
||||||
|
run: |
|
||||||
|
cmake . -D BUILD_SHARED_LIBS=ON -D QHOTKEY_EXAMPLES=ON ${{ matrix.additional_arguments }}
|
||||||
|
cmake --build .
|
||||||
|
|
||||||
|
- name: Setup MSVC environment for QMake
|
||||||
|
uses: ilammy/msvc-dev-cmd@v1
|
||||||
|
|
||||||
|
- name: Build with QMake as static
|
||||||
|
working-directory: HotkeyTest
|
||||||
|
run: |
|
||||||
|
qmake
|
||||||
|
${{ matrix.make }}
|
||||||
|
|
||||||
|
- name: Build with QMake as shared
|
||||||
|
working-directory: HotkeyTest
|
||||||
|
run: |
|
||||||
|
qmake "DEFINES+=QHOTKEY_SHARED QHOTKEY_LIBRARY"
|
||||||
|
${{ matrix.make }}
|
||||||
+46
-23
@@ -1,61 +1,81 @@
|
|||||||
cmake_minimum_required(VERSION 3.1)
|
cmake_minimum_required(VERSION 3.1)
|
||||||
|
|
||||||
project(qhotkey VERSION 1.2.2 LANGUAGES CXX)
|
project(qhotkey VERSION 1.5.0 LANGUAGES CXX)
|
||||||
|
|
||||||
option(QHOTKEY_EXAMPLES "Build examples" OFF)
|
option(QHOTKEY_EXAMPLES "Build examples" OFF)
|
||||||
option(QHOTKEY_INSTALL "Enable install rule" ON)
|
option(QHOTKEY_INSTALL "Enable install rule" ON)
|
||||||
|
|
||||||
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
|
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
|
||||||
|
set(CMAKE_AUTOMOC ON)
|
||||||
|
|
||||||
find_package(Qt5 COMPONENTS Core Widgets REQUIRED)
|
if(NOT QT_DEFAULT_MAJOR_VERSION)
|
||||||
|
set(QT_DEFAULT_MAJOR_VERSION 5 CACHE STRING "Qt version to use (5 or 6), defaults to 5")
|
||||||
|
endif()
|
||||||
|
|
||||||
qt5_wrap_cpp(MOC_HEADERS
|
if(QT_DEFAULT_MAJOR_VERSION EQUAL 6)
|
||||||
QHotkey/qhotkey.h
|
find_package(Qt${QT_DEFAULT_MAJOR_VERSION} 6.2.0 COMPONENTS Core Gui REQUIRED)
|
||||||
QHotkey/qhotkey_p.h)
|
else()
|
||||||
|
find_package(Qt${QT_DEFAULT_MAJOR_VERSION} COMPONENTS Core Gui REQUIRED)
|
||||||
|
endif()
|
||||||
|
|
||||||
set(LIBS
|
add_library(qhotkey QHotkey/qhotkey.cpp)
|
||||||
Qt5::Core
|
add_library(QHotkey::QHotkey ALIAS qhotkey)
|
||||||
Qt5::Widgets)
|
target_link_libraries(qhotkey PUBLIC Qt${QT_DEFAULT_MAJOR_VERSION}::Core Qt${QT_DEFAULT_MAJOR_VERSION}::Gui)
|
||||||
|
|
||||||
set(SRC_FILES
|
if(BUILD_SHARED_LIBS)
|
||||||
QHotkey/qhotkey.cpp)
|
target_compile_definitions(qhotkey PRIVATE QHOTKEY_LIBRARY)
|
||||||
|
target_compile_definitions(qhotkey PUBLIC QHOTKEY_SHARED)
|
||||||
|
endif()
|
||||||
|
|
||||||
if(APPLE)
|
if(APPLE)
|
||||||
find_library(CARBON_LIBRARY Carbon)
|
find_library(CARBON_LIBRARY Carbon)
|
||||||
mark_as_advanced(CARBON_LIBRARY)
|
mark_as_advanced(CARBON_LIBRARY)
|
||||||
|
|
||||||
set(SRC_FILES ${SRC_FILES} QHotkey/qhotkey_mac.cpp)
|
target_sources(qhotkey PRIVATE QHotkey/qhotkey_mac.cpp)
|
||||||
set(LIBS ${LIBS} ${CARBON_LIBRARY})
|
target_link_libraries(qhotkey PRIVATE ${CARBON_LIBRARY})
|
||||||
elseif(WIN32)
|
elseif(WIN32)
|
||||||
set(SRC_FILES ${SRC_FILES} QHotkey/qhotkey_win.cpp)
|
target_sources(qhotkey PRIVATE QHotkey/qhotkey_win.cpp)
|
||||||
else()
|
else()
|
||||||
find_package(X11 REQUIRED)
|
find_package(X11 REQUIRED)
|
||||||
find_package(Qt5X11Extras REQUIRED)
|
if(QT_DEFAULT_MAJOR_VERSION GREATER_EQUAL 6)
|
||||||
|
target_link_libraries(qhotkey PRIVATE ${X11_LIBRARIES})
|
||||||
|
else()
|
||||||
|
find_package(Qt${QT_DEFAULT_MAJOR_VERSION} COMPONENTS X11Extras REQUIRED)
|
||||||
|
target_link_libraries(qhotkey
|
||||||
|
PRIVATE
|
||||||
|
${X11_LIBRARIES}
|
||||||
|
Qt${QT_DEFAULT_MAJOR_VERSION}::X11Extras)
|
||||||
|
endif()
|
||||||
|
|
||||||
include_directories(${X11_INCLUDE_DIR})
|
include_directories(${X11_INCLUDE_DIR})
|
||||||
set(LIBS ${LIBS} ${X11_LIBRARIES} Qt5::X11Extras)
|
target_sources(qhotkey PRIVATE QHotkey/qhotkey_x11.cpp)
|
||||||
set(SRC_FILES ${SRC_FILES} QHotkey/qhotkey_x11.cpp)
|
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
add_library(qhotkey ${SRC_FILES} ${MOC_HEADERS})
|
include(GNUInstallDirs)
|
||||||
add_library(QHotkey::QHotkey ALIAS qhotkey)
|
|
||||||
target_link_libraries(qhotkey ${LIBS})
|
|
||||||
|
|
||||||
target_include_directories(qhotkey
|
target_include_directories(qhotkey
|
||||||
PUBLIC
|
PUBLIC
|
||||||
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/QHotkey>
|
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/QHotkey>
|
||||||
$<INSTALL_INTERFACE:include>)
|
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
|
||||||
|
|
||||||
|
include(CMakePackageConfigHelpers)
|
||||||
|
|
||||||
set_target_properties(qhotkey PROPERTIES
|
set_target_properties(qhotkey PROPERTIES
|
||||||
SOVERSION ${PROJECT_VERSION_MAJOR}
|
SOVERSION ${PROJECT_VERSION_MAJOR}
|
||||||
VERSION ${PROJECT_VERSION})
|
VERSION ${PROJECT_VERSION}
|
||||||
|
INTERFACE_QHotkey_MAJOR_VERSION ${PROJECT_VERSION_MAJOR}
|
||||||
|
COMPATIBLE_INTERFACE_STRING QHotkey_MAJOR_VERSION)
|
||||||
|
|
||||||
|
write_basic_package_version_file(
|
||||||
|
${CMAKE_CURRENT_BINARY_DIR}/QHotkeyConfigVersion.cmake
|
||||||
|
VERSION "${PROJECT_VERSION}"
|
||||||
|
COMPATIBILITY AnyNewerVersion)
|
||||||
|
|
||||||
if(QHOTKEY_EXAMPLES)
|
if(QHOTKEY_EXAMPLES)
|
||||||
add_subdirectory(HotkeyTest)
|
add_subdirectory(HotkeyTest)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
if(QHOTKEY_INSTALL)
|
if(QHOTKEY_INSTALL)
|
||||||
include(GNUInstallDirs)
|
|
||||||
set(INSTALL_CONFIGDIR ${CMAKE_INSTALL_LIBDIR}/cmake/QHotkey)
|
set(INSTALL_CONFIGDIR ${CMAKE_INSTALL_LIBDIR}/cmake/QHotkey)
|
||||||
|
|
||||||
install(
|
install(
|
||||||
@@ -66,7 +86,10 @@ if(QHOTKEY_INSTALL)
|
|||||||
install(FILES
|
install(FILES
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/QHotkey/qhotkey.h
|
${CMAKE_CURRENT_SOURCE_DIR}/QHotkey/qhotkey.h
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/QHotkey/QHotkey
|
${CMAKE_CURRENT_SOURCE_DIR}/QHotkey/QHotkey
|
||||||
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/)
|
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
|
||||||
|
install(FILES
|
||||||
|
${CMAKE_CURRENT_BINARY_DIR}/QHotkeyConfigVersion.cmake
|
||||||
|
DESTINATION ${INSTALL_CONFIGDIR})
|
||||||
install(EXPORT QHotkeyConfig DESTINATION ${INSTALL_CONFIGDIR})
|
install(EXPORT QHotkeyConfig DESTINATION ${INSTALL_CONFIGDIR})
|
||||||
|
|
||||||
export(TARGETS qhotkey FILE QHotkeyConfig.cmake)
|
export(TARGETS qhotkey FILE QHotkeyConfig.cmake)
|
||||||
|
|||||||
@@ -1,12 +1,11 @@
|
|||||||
set(CMAKE_INCLUDE_CURRENT_DIR ON)
|
set(CMAKE_INCLUDE_CURRENT_DIR ON)
|
||||||
|
|
||||||
qt5_wrap_ui(test_UI_HEADERS hottestwidget.ui)
|
set(CMAKE_AUTOUIC ON)
|
||||||
qt5_wrap_cpp(test_MOC_HEADERS hottestwidget.h)
|
|
||||||
|
|
||||||
add_executable(HotkeyTest
|
add_executable(HotkeyTest
|
||||||
main.cpp
|
main.cpp
|
||||||
hottestwidget.cpp
|
hottestwidget.cpp
|
||||||
${test_UI_HEADERS}
|
hottestwidget.ui)
|
||||||
${test_MOC_HEADERS})
|
|
||||||
target_link_libraries(HotkeyTest Qt5::Widgets qhotkey)
|
find_package(Qt${QT_DEFAULT_MAJOR_VERSION} COMPONENTS Widgets REQUIRED)
|
||||||
target_include_directories(HotkeyTest PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
|
target_link_libraries(HotkeyTest Qt${QT_DEFAULT_MAJOR_VERSION}::Widgets QHotkey::QHotkey)
|
||||||
|
|||||||
+2
-2
@@ -2,11 +2,11 @@ TEMPLATE = lib
|
|||||||
win32: CONFIG += dll
|
win32: CONFIG += dll
|
||||||
|
|
||||||
TARGET = QHotkey
|
TARGET = QHotkey
|
||||||
VERSION = 1.2.1
|
VERSION = 1.5.0
|
||||||
|
|
||||||
include(../qhotkey.pri)
|
include(../qhotkey.pri)
|
||||||
|
|
||||||
DEFINES += QHOTKEY_LIB QHOTKEY_LIB_BUILD
|
DEFINES += QHOTKEY_SHARED QHOTKEY_LIBRARY
|
||||||
|
|
||||||
# use INSTALL_ROOT to modify the install location
|
# use INSTALL_ROOT to modify the install location
|
||||||
headers.files = $$PUBLIC_HEADERS
|
headers.files = $$PUBLIC_HEADERS
|
||||||
|
|||||||
+57
-40
@@ -8,11 +8,17 @@
|
|||||||
|
|
||||||
Q_LOGGING_CATEGORY(logQHotkey, "QHotkey")
|
Q_LOGGING_CATEGORY(logQHotkey, "QHotkey")
|
||||||
|
|
||||||
void QHotkey::addGlobalMapping(const QKeySequence &shortcut, const QHotkey::NativeShortcut &nativeShortcut)
|
void QHotkey::addGlobalMapping(const QKeySequence &shortcut, QHotkey::NativeShortcut nativeShortcut)
|
||||||
{
|
{
|
||||||
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
|
||||||
|
const int key = shortcut[0].toCombined();
|
||||||
|
#else
|
||||||
|
const int key = shortcut[0];
|
||||||
|
#endif
|
||||||
|
|
||||||
QMetaObject::invokeMethod(QHotkeyPrivate::instance(), "addMappingInvoked", Qt::QueuedConnection,
|
QMetaObject::invokeMethod(QHotkeyPrivate::instance(), "addMappingInvoked", Qt::QueuedConnection,
|
||||||
Q_ARG(Qt::Key, Qt::Key(shortcut[0] & ~Qt::KeyboardModifierMask)),
|
Q_ARG(Qt::Key, Qt::Key(key & ~Qt::KeyboardModifierMask)),
|
||||||
Q_ARG(Qt::KeyboardModifiers, Qt::KeyboardModifiers(shortcut[0] & Qt::KeyboardModifierMask)),
|
Q_ARG(Qt::KeyboardModifiers, Qt::KeyboardModifiers(key & Qt::KeyboardModifierMask)),
|
||||||
Q_ARG(QHotkey::NativeShortcut, nativeShortcut));
|
Q_ARG(QHotkey::NativeShortcut, nativeShortcut));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -25,14 +31,13 @@ QHotkey::QHotkey(QObject *parent) :
|
|||||||
QObject(parent),
|
QObject(parent),
|
||||||
_keyCode(Qt::Key_unknown),
|
_keyCode(Qt::Key_unknown),
|
||||||
_modifiers(Qt::NoModifier),
|
_modifiers(Qt::NoModifier),
|
||||||
_nativeShortcut(),
|
|
||||||
_registered(false)
|
_registered(false)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
QHotkey::QHotkey(const QKeySequence &sequence, bool autoRegister, QObject *parent) :
|
QHotkey::QHotkey(const QKeySequence &shortcut, bool autoRegister, QObject *parent) :
|
||||||
QHotkey(parent)
|
QHotkey(parent)
|
||||||
{
|
{
|
||||||
setShortcut(sequence, autoRegister);
|
setShortcut(shortcut, autoRegister);
|
||||||
}
|
}
|
||||||
|
|
||||||
QHotkey::QHotkey(Qt::Key keyCode, Qt::KeyboardModifiers modifiers, bool autoRegister, QObject *parent) :
|
QHotkey::QHotkey(Qt::Key keyCode, Qt::KeyboardModifiers modifiers, bool autoRegister, QObject *parent) :
|
||||||
@@ -41,7 +46,7 @@ QHotkey::QHotkey(Qt::Key keyCode, Qt::KeyboardModifiers modifiers, bool autoRegi
|
|||||||
setShortcut(keyCode, modifiers, autoRegister);
|
setShortcut(keyCode, modifiers, autoRegister);
|
||||||
}
|
}
|
||||||
|
|
||||||
QHotkey::QHotkey(const QHotkey::NativeShortcut &shortcut, bool autoRegister, QObject *parent) :
|
QHotkey::QHotkey(QHotkey::NativeShortcut shortcut, bool autoRegister, QObject *parent) :
|
||||||
QHotkey(parent)
|
QHotkey(parent)
|
||||||
{
|
{
|
||||||
setNativeShortcut(shortcut, autoRegister);
|
setNativeShortcut(shortcut, autoRegister);
|
||||||
@@ -57,8 +62,12 @@ QKeySequence QHotkey::shortcut() const
|
|||||||
{
|
{
|
||||||
if(_keyCode == Qt::Key_unknown)
|
if(_keyCode == Qt::Key_unknown)
|
||||||
return QKeySequence();
|
return QKeySequence();
|
||||||
else
|
|
||||||
return QKeySequence(_keyCode | _modifiers);
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
|
||||||
|
return QKeySequence((_keyCode | _modifiers).toCombined());
|
||||||
|
#else
|
||||||
|
return QKeySequence(static_cast<int>(_keyCode | _modifiers));
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
Qt::Key QHotkey::keyCode() const
|
Qt::Key QHotkey::keyCode() const
|
||||||
@@ -83,15 +92,21 @@ bool QHotkey::isRegistered() const
|
|||||||
|
|
||||||
bool QHotkey::setShortcut(const QKeySequence &shortcut, bool autoRegister)
|
bool QHotkey::setShortcut(const QKeySequence &shortcut, bool autoRegister)
|
||||||
{
|
{
|
||||||
if(shortcut.isEmpty()) {
|
if(shortcut.isEmpty())
|
||||||
return resetShortcut();
|
return resetShortcut();
|
||||||
} else if(shortcut.count() > 1) {
|
if(shortcut.count() > 1) {
|
||||||
qCWarning(logQHotkey, "Keysequences with multiple shortcuts are not allowed! "
|
qCWarning(logQHotkey, "Keysequences with multiple shortcuts are not allowed! "
|
||||||
"Only the first shortcut will be used!");
|
"Only the first shortcut will be used!");
|
||||||
}
|
}
|
||||||
|
|
||||||
return setShortcut(Qt::Key(shortcut[0] & ~Qt::KeyboardModifierMask),
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
|
||||||
Qt::KeyboardModifiers(shortcut[0] & Qt::KeyboardModifierMask),
|
const int key = shortcut[0].toCombined();
|
||||||
|
#else
|
||||||
|
const int key = shortcut[0];
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return setShortcut(Qt::Key(key & ~Qt::KeyboardModifierMask),
|
||||||
|
Qt::KeyboardModifiers(key & Qt::KeyboardModifierMask),
|
||||||
autoRegister);
|
autoRegister);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -118,15 +133,14 @@ bool QHotkey::setShortcut(Qt::Key keyCode, Qt::KeyboardModifiers modifiers, bool
|
|||||||
if(_nativeShortcut.isValid()) {
|
if(_nativeShortcut.isValid()) {
|
||||||
if(autoRegister)
|
if(autoRegister)
|
||||||
return QHotkeyPrivate::instance()->addShortcut(this);
|
return QHotkeyPrivate::instance()->addShortcut(this);
|
||||||
else
|
|
||||||
return true;
|
return true;
|
||||||
} else {
|
}
|
||||||
|
|
||||||
qCWarning(logQHotkey) << "Unable to map shortcut to native keys. Key:" << keyCode << "Modifiers:" << modifiers;
|
qCWarning(logQHotkey) << "Unable to map shortcut to native keys. Key:" << keyCode << "Modifiers:" << modifiers;
|
||||||
_keyCode = Qt::Key_unknown;
|
_keyCode = Qt::Key_unknown;
|
||||||
_modifiers = Qt::NoModifier;
|
_modifiers = Qt::NoModifier;
|
||||||
_nativeShortcut = NativeShortcut();
|
_nativeShortcut = NativeShortcut();
|
||||||
return false;
|
return false;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool QHotkey::resetShortcut()
|
bool QHotkey::resetShortcut()
|
||||||
@@ -158,26 +172,24 @@ bool QHotkey::setNativeShortcut(QHotkey::NativeShortcut nativeShortcut, bool aut
|
|||||||
_nativeShortcut = nativeShortcut;
|
_nativeShortcut = nativeShortcut;
|
||||||
if(autoRegister)
|
if(autoRegister)
|
||||||
return QHotkeyPrivate::instance()->addShortcut(this);
|
return QHotkeyPrivate::instance()->addShortcut(this);
|
||||||
else
|
|
||||||
return true;
|
return true;
|
||||||
} else {
|
}
|
||||||
|
|
||||||
_keyCode = Qt::Key_unknown;
|
_keyCode = Qt::Key_unknown;
|
||||||
_modifiers = Qt::NoModifier;
|
_modifiers = Qt::NoModifier;
|
||||||
_nativeShortcut = NativeShortcut();
|
_nativeShortcut = NativeShortcut();
|
||||||
return true;
|
return true;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool QHotkey::setRegistered(bool registered)
|
bool QHotkey::setRegistered(bool registered)
|
||||||
{
|
{
|
||||||
if(_registered && !registered)
|
if(_registered && !registered)
|
||||||
return QHotkeyPrivate::instance()->removeShortcut(this);
|
return QHotkeyPrivate::instance()->removeShortcut(this);
|
||||||
else if(!_registered && registered) {
|
if(!_registered && registered) {
|
||||||
if(!_nativeShortcut.isValid())
|
if(!_nativeShortcut.isValid())
|
||||||
return false;
|
return false;
|
||||||
else
|
|
||||||
return QHotkeyPrivate::instance()->addShortcut(this);
|
return QHotkeyPrivate::instance()->addShortcut(this);
|
||||||
} else
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -185,8 +197,7 @@ bool QHotkey::setRegistered(bool registered)
|
|||||||
|
|
||||||
// ---------- QHotkeyPrivate implementation ----------
|
// ---------- QHotkeyPrivate implementation ----------
|
||||||
|
|
||||||
QHotkeyPrivate::QHotkeyPrivate() :
|
QHotkeyPrivate::QHotkeyPrivate()
|
||||||
shortcuts()
|
|
||||||
{
|
{
|
||||||
Q_ASSERT_X(qApp, Q_FUNC_INFO, "QHotkey requires QCoreApplication to be instantiated");
|
Q_ASSERT_X(qApp, Q_FUNC_INFO, "QHotkey requires QCoreApplication to be instantiated");
|
||||||
qApp->eventDispatcher()->installNativeEventFilter(this);
|
qApp->eventDispatcher()->installNativeEventFilter(this);
|
||||||
@@ -211,7 +222,7 @@ QHotkey::NativeShortcut QHotkeyPrivate::nativeShortcut(Qt::Key keycode, Qt::Keyb
|
|||||||
Q_ARG(Qt::Key, keycode),
|
Q_ARG(Qt::Key, keycode),
|
||||||
Q_ARG(Qt::KeyboardModifiers, modifiers))) {
|
Q_ARG(Qt::KeyboardModifiers, modifiers))) {
|
||||||
return QHotkey::NativeShortcut();
|
return QHotkey::NativeShortcut();
|
||||||
} else
|
}
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -228,11 +239,11 @@ bool QHotkeyPrivate::addShortcut(QHotkey *hotkey)
|
|||||||
Q_RETURN_ARG(bool, res),
|
Q_RETURN_ARG(bool, res),
|
||||||
Q_ARG(QHotkey*, hotkey))) {
|
Q_ARG(QHotkey*, hotkey))) {
|
||||||
return false;
|
return false;
|
||||||
} else {
|
}
|
||||||
|
|
||||||
if(res)
|
if(res)
|
||||||
emit hotkey->registeredChanged(true);
|
emit hotkey->registeredChanged(true);
|
||||||
return res;
|
return res;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool QHotkeyPrivate::removeShortcut(QHotkey *hotkey)
|
bool QHotkeyPrivate::removeShortcut(QHotkey *hotkey)
|
||||||
@@ -248,11 +259,11 @@ bool QHotkeyPrivate::removeShortcut(QHotkey *hotkey)
|
|||||||
Q_RETURN_ARG(bool, res),
|
Q_RETURN_ARG(bool, res),
|
||||||
Q_ARG(QHotkey*, hotkey))) {
|
Q_ARG(QHotkey*, hotkey))) {
|
||||||
return false;
|
return false;
|
||||||
} else {
|
}
|
||||||
|
|
||||||
if(res)
|
if(res)
|
||||||
emit hotkey->registeredChanged(false);
|
emit hotkey->registeredChanged(false);
|
||||||
return res;
|
return res;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void QHotkeyPrivate::activateShortcut(QHotkey::NativeShortcut shortcut)
|
void QHotkeyPrivate::activateShortcut(QHotkey::NativeShortcut shortcut)
|
||||||
@@ -269,7 +280,7 @@ void QHotkeyPrivate::releaseShortcut(QHotkey::NativeShortcut shortcut)
|
|||||||
signal.invoke(hkey, Qt::QueuedConnection);
|
signal.invoke(hkey, Qt::QueuedConnection);
|
||||||
}
|
}
|
||||||
|
|
||||||
void QHotkeyPrivate::addMappingInvoked(Qt::Key keycode, Qt::KeyboardModifiers modifiers, const QHotkey::NativeShortcut &nativeShortcut)
|
void QHotkeyPrivate::addMappingInvoked(Qt::Key keycode, Qt::KeyboardModifiers modifiers, QHotkey::NativeShortcut nativeShortcut)
|
||||||
{
|
{
|
||||||
mapping.insert({keycode, modifiers}, nativeShortcut);
|
mapping.insert({keycode, modifiers}, nativeShortcut);
|
||||||
}
|
}
|
||||||
@@ -279,9 +290,11 @@ bool QHotkeyPrivate::addShortcutInvoked(QHotkey *hotkey)
|
|||||||
QHotkey::NativeShortcut shortcut = hotkey->_nativeShortcut;
|
QHotkey::NativeShortcut shortcut = hotkey->_nativeShortcut;
|
||||||
|
|
||||||
if(!shortcuts.contains(shortcut)) {
|
if(!shortcuts.contains(shortcut)) {
|
||||||
if(!registerShortcut(shortcut))
|
if(!registerShortcut(shortcut)) {
|
||||||
|
qCWarning(logQHotkey) << QHotkey::tr("Failed to register %1. Error: %2").arg(hotkey->shortcut().toString(), error);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
shortcuts.insert(shortcut, hotkey);
|
shortcuts.insert(shortcut, hotkey);
|
||||||
hotkey->_registered = true;
|
hotkey->_registered = true;
|
||||||
@@ -296,9 +309,13 @@ bool QHotkeyPrivate::removeShortcutInvoked(QHotkey *hotkey)
|
|||||||
return false;
|
return false;
|
||||||
hotkey->_registered = false;
|
hotkey->_registered = false;
|
||||||
emit hotkey->registeredChanged(true);
|
emit hotkey->registeredChanged(true);
|
||||||
if(shortcuts.count(shortcut) == 0)
|
if(shortcuts.count(shortcut) == 0) {
|
||||||
return unregisterShortcut(shortcut);
|
if (!unregisterShortcut(shortcut)) {
|
||||||
else
|
qCWarning(logQHotkey) << QHotkey::tr("Failed to unregister %1. Error: %2").arg(hotkey->shortcut().toString(), error);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -307,12 +324,12 @@ QHotkey::NativeShortcut QHotkeyPrivate::nativeShortcutInvoked(Qt::Key keycode, Q
|
|||||||
if(mapping.contains({keycode, modifiers}))
|
if(mapping.contains({keycode, modifiers}))
|
||||||
return mapping.value({keycode, modifiers});
|
return mapping.value({keycode, modifiers});
|
||||||
|
|
||||||
bool ok1, ok2 = false;
|
bool ok1 = false;
|
||||||
auto k = nativeKeycode(keycode, ok1);
|
auto k = nativeKeycode(keycode, ok1);
|
||||||
|
bool ok2 = false;
|
||||||
auto m = nativeModifiers(modifiers, ok2);
|
auto m = nativeModifiers(modifiers, ok2);
|
||||||
if(ok1 && ok2)
|
if(ok1 && ok2)
|
||||||
return {k, m};
|
return {k, m};
|
||||||
else
|
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -335,26 +352,26 @@ bool QHotkey::NativeShortcut::isValid() const
|
|||||||
return valid;
|
return valid;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool QHotkey::NativeShortcut::operator ==(const QHotkey::NativeShortcut &other) const
|
bool QHotkey::NativeShortcut::operator ==(QHotkey::NativeShortcut other) const
|
||||||
{
|
{
|
||||||
return (key == other.key) &&
|
return (key == other.key) &&
|
||||||
(modifier == other.modifier) &&
|
(modifier == other.modifier) &&
|
||||||
valid == other.valid;
|
valid == other.valid;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool QHotkey::NativeShortcut::operator !=(const QHotkey::NativeShortcut &other) const
|
bool QHotkey::NativeShortcut::operator !=(QHotkey::NativeShortcut other) const
|
||||||
{
|
{
|
||||||
return (key != other.key) ||
|
return (key != other.key) ||
|
||||||
(modifier != other.modifier) ||
|
(modifier != other.modifier) ||
|
||||||
valid != other.valid;
|
valid != other.valid;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint qHash(const QHotkey::NativeShortcut &key)
|
QHOTKEY_HASH_SEED qHash(QHotkey::NativeShortcut key)
|
||||||
{
|
{
|
||||||
return qHash(key.key) ^ qHash(key.modifier);
|
return qHash(key.key) ^ qHash(key.modifier);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint qHash(const QHotkey::NativeShortcut &key, uint seed)
|
QHOTKEY_HASH_SEED qHash(QHotkey::NativeShortcut key, QHOTKEY_HASH_SEED seed)
|
||||||
{
|
{
|
||||||
return qHash(key.key, seed) ^ qHash(key.modifier, seed);
|
return qHash(key.key, seed) ^ qHash(key.modifier, seed);
|
||||||
}
|
}
|
||||||
|
|||||||
+24
-18
@@ -6,18 +6,24 @@
|
|||||||
#include <QPair>
|
#include <QPair>
|
||||||
#include <QLoggingCategory>
|
#include <QLoggingCategory>
|
||||||
|
|
||||||
#ifdef QHOTKEY_LIB
|
#ifdef QHOTKEY_SHARED
|
||||||
#ifdef QHOTKEY_LIB_BUILD
|
# ifdef QHOTKEY_LIBRARY
|
||||||
#define QHOTKEY_SHARED_EXPORT Q_DECL_EXPORT
|
# define QHOTKEY_EXPORT Q_DECL_EXPORT
|
||||||
#else
|
# else
|
||||||
#define QHOTKEY_SHARED_EXPORT Q_DECL_IMPORT
|
# define QHOTKEY_EXPORT Q_DECL_IMPORT
|
||||||
#endif
|
# endif
|
||||||
#else
|
#else
|
||||||
#define QHOTKEY_SHARED_EXPORT
|
# define QHOTKEY_EXPORT
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
|
||||||
|
#define QHOTKEY_HASH_SEED size_t
|
||||||
|
#else
|
||||||
|
#define QHOTKEY_HASH_SEED uint
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
//! A class to define global, systemwide Hotkeys
|
//! A class to define global, systemwide Hotkeys
|
||||||
class QHOTKEY_SHARED_EXPORT QHotkey : public QObject
|
class QHOTKEY_EXPORT QHotkey : public QObject
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
//! @private
|
//! @private
|
||||||
@@ -30,7 +36,7 @@ class QHOTKEY_SHARED_EXPORT QHotkey : public QObject
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
//! Defines shortcut with native keycodes
|
//! Defines shortcut with native keycodes
|
||||||
class QHOTKEY_SHARED_EXPORT NativeShortcut {
|
class QHOTKEY_EXPORT NativeShortcut {
|
||||||
public:
|
public:
|
||||||
//! The native keycode
|
//! The native keycode
|
||||||
quint32 key;
|
quint32 key;
|
||||||
@@ -46,16 +52,16 @@ public:
|
|||||||
bool isValid() const;
|
bool isValid() const;
|
||||||
|
|
||||||
//! Equality operator
|
//! Equality operator
|
||||||
bool operator ==(const NativeShortcut &other) const;
|
bool operator ==(NativeShortcut other) const;
|
||||||
//! Inequality operator
|
//! Inequality operator
|
||||||
bool operator !=(const NativeShortcut &other) const;
|
bool operator !=(NativeShortcut other) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool valid;
|
bool valid;
|
||||||
};
|
};
|
||||||
|
|
||||||
//! Adds a global mapping of a key sequence to a replacement native shortcut
|
//! Adds a global mapping of a key sequence to a replacement native shortcut
|
||||||
static void addGlobalMapping(const QKeySequence &shortcut, const NativeShortcut &nativeShortcut);
|
static void addGlobalMapping(const QKeySequence &shortcut, NativeShortcut nativeShortcut);
|
||||||
|
|
||||||
//! Checks if global shortcuts are supported by the current platform
|
//! Checks if global shortcuts are supported by the current platform
|
||||||
static bool isPlatformSupported();
|
static bool isPlatformSupported();
|
||||||
@@ -67,8 +73,8 @@ public:
|
|||||||
//! Constructs a hotkey with a key and modifiers and optionally registers it
|
//! Constructs a hotkey with a key and modifiers and optionally registers it
|
||||||
explicit QHotkey(Qt::Key keyCode, Qt::KeyboardModifiers modifiers, bool autoRegister = false, QObject *parent = nullptr);
|
explicit QHotkey(Qt::Key keyCode, Qt::KeyboardModifiers modifiers, bool autoRegister = false, QObject *parent = nullptr);
|
||||||
//! Constructs a hotkey from a native shortcut and optionally registers it
|
//! Constructs a hotkey from a native shortcut and optionally registers it
|
||||||
explicit QHotkey(const NativeShortcut &shortcut, bool autoRegister = false, QObject *parent = nullptr);
|
explicit QHotkey(NativeShortcut shortcut, bool autoRegister = false, QObject *parent = nullptr);
|
||||||
~QHotkey();
|
~QHotkey() override;
|
||||||
|
|
||||||
//! @readAcFn{QHotkey::registered}
|
//! @readAcFn{QHotkey::registered}
|
||||||
bool isRegistered() const;
|
bool isRegistered() const;
|
||||||
@@ -94,7 +100,7 @@ public slots:
|
|||||||
bool resetShortcut();
|
bool resetShortcut();
|
||||||
|
|
||||||
//! Set this hotkey to a native shortcut
|
//! Set this hotkey to a native shortcut
|
||||||
bool setNativeShortcut(NativeShortcut nativeShortcut, bool autoRegister = false);
|
bool setNativeShortcut(QHotkey::NativeShortcut nativeShortcut, bool autoRegister = false);
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
//! Will be emitted if the shortcut is pressed
|
//! Will be emitted if the shortcut is pressed
|
||||||
@@ -114,10 +120,10 @@ private:
|
|||||||
bool _registered;
|
bool _registered;
|
||||||
};
|
};
|
||||||
|
|
||||||
uint QHOTKEY_SHARED_EXPORT qHash(const QHotkey::NativeShortcut &key);
|
QHOTKEY_HASH_SEED QHOTKEY_EXPORT qHash(QHotkey::NativeShortcut key);
|
||||||
uint QHOTKEY_SHARED_EXPORT qHash(const QHotkey::NativeShortcut &key, uint seed);
|
QHOTKEY_HASH_SEED QHOTKEY_EXPORT qHash(QHotkey::NativeShortcut key, QHOTKEY_HASH_SEED seed);
|
||||||
|
|
||||||
QHOTKEY_SHARED_EXPORT Q_DECLARE_LOGGING_CATEGORY(logQHotkey)
|
QHOTKEY_EXPORT Q_DECLARE_LOGGING_CATEGORY(logQHotkey)
|
||||||
|
|
||||||
Q_DECLARE_METATYPE(QHotkey::NativeShortcut)
|
Q_DECLARE_METATYPE(QHotkey::NativeShortcut)
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ class QHotkeyPrivateMac : public QHotkeyPrivate
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
// QAbstractNativeEventFilter interface
|
// QAbstractNativeEventFilter interface
|
||||||
bool nativeEventFilter(const QByteArray &eventType, void *message, long *result) Q_DECL_OVERRIDE;
|
bool nativeEventFilter(const QByteArray &eventType, void *message, _NATIVE_EVENT_RESULT *result) override;
|
||||||
|
|
||||||
static OSStatus hotkeyPressEventHandler(EventHandlerCallRef nextHandler, EventRef event, void* data);
|
static OSStatus hotkeyPressEventHandler(EventHandlerCallRef nextHandler, EventRef event, void* data);
|
||||||
static OSStatus hotkeyReleaseEventHandler(EventHandlerCallRef nextHandler, EventRef event, void* data);
|
static OSStatus hotkeyReleaseEventHandler(EventHandlerCallRef nextHandler, EventRef event, void* data);
|
||||||
@@ -33,7 +33,7 @@ bool QHotkeyPrivate::isPlatformSupported()
|
|||||||
bool QHotkeyPrivateMac::isHotkeyHandlerRegistered = false;
|
bool QHotkeyPrivateMac::isHotkeyHandlerRegistered = false;
|
||||||
QHash<QHotkey::NativeShortcut, EventHotKeyRef> QHotkeyPrivateMac::hotkeyRefs;
|
QHash<QHotkey::NativeShortcut, EventHotKeyRef> QHotkeyPrivateMac::hotkeyRefs;
|
||||||
|
|
||||||
bool QHotkeyPrivateMac::nativeEventFilter(const QByteArray &eventType, void *message, long *result)
|
bool QHotkeyPrivateMac::nativeEventFilter(const QByteArray &eventType, void *message, _NATIVE_EVENT_RESULT *result)
|
||||||
{
|
{
|
||||||
Q_UNUSED(eventType)
|
Q_UNUSED(eventType)
|
||||||
Q_UNUSED(message)
|
Q_UNUSED(message)
|
||||||
@@ -227,8 +227,7 @@ bool QHotkeyPrivateMac::registerShortcut(QHotkey::NativeShortcut shortcut)
|
|||||||
0,
|
0,
|
||||||
&eventRef);
|
&eventRef);
|
||||||
if (status != noErr) {
|
if (status != noErr) {
|
||||||
qCWarning(logQHotkey) << "Failed to register hotkey. Error:"
|
error = QString::number(status);
|
||||||
<< status;
|
|
||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
this->hotkeyRefs.insert(shortcut, eventRef);
|
this->hotkeyRefs.insert(shortcut, eventRef);
|
||||||
@@ -241,8 +240,7 @@ bool QHotkeyPrivateMac::unregisterShortcut(QHotkey::NativeShortcut shortcut)
|
|||||||
EventHotKeyRef eventRef = QHotkeyPrivateMac::hotkeyRefs.value(shortcut);
|
EventHotKeyRef eventRef = QHotkeyPrivateMac::hotkeyRefs.value(shortcut);
|
||||||
OSStatus status = UnregisterEventHotKey(eventRef);
|
OSStatus status = UnregisterEventHotKey(eventRef);
|
||||||
if (status != noErr) {
|
if (status != noErr) {
|
||||||
qCWarning(logQHotkey) << "Failed to unregister hotkey. Error:"
|
error = QString::number(status);
|
||||||
<< status;
|
|
||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
this->hotkeyRefs.remove(shortcut);
|
this->hotkeyRefs.remove(shortcut);
|
||||||
|
|||||||
+10
-5
@@ -7,7 +7,13 @@
|
|||||||
#include <QMutex>
|
#include <QMutex>
|
||||||
#include <QGlobalStatic>
|
#include <QGlobalStatic>
|
||||||
|
|
||||||
class QHOTKEY_SHARED_EXPORT QHotkeyPrivate : public QObject, public QAbstractNativeEventFilter
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
|
||||||
|
#define _NATIVE_EVENT_RESULT qintptr
|
||||||
|
#else
|
||||||
|
#define _NATIVE_EVENT_RESULT long
|
||||||
|
#endif
|
||||||
|
|
||||||
|
class QHOTKEY_EXPORT QHotkeyPrivate : public QObject, public QAbstractNativeEventFilter
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
@@ -33,11 +39,13 @@ protected:
|
|||||||
virtual bool registerShortcut(QHotkey::NativeShortcut shortcut) = 0;//platform implement
|
virtual bool registerShortcut(QHotkey::NativeShortcut shortcut) = 0;//platform implement
|
||||||
virtual bool unregisterShortcut(QHotkey::NativeShortcut shortcut) = 0;//platform implement
|
virtual bool unregisterShortcut(QHotkey::NativeShortcut shortcut) = 0;//platform implement
|
||||||
|
|
||||||
|
QString error;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QHash<QPair<Qt::Key, Qt::KeyboardModifiers>, QHotkey::NativeShortcut> mapping;
|
QHash<QPair<Qt::Key, Qt::KeyboardModifiers>, QHotkey::NativeShortcut> mapping;
|
||||||
QMultiHash<QHotkey::NativeShortcut, QHotkey*> shortcuts;
|
QMultiHash<QHotkey::NativeShortcut, QHotkey*> shortcuts;
|
||||||
|
|
||||||
Q_INVOKABLE void addMappingInvoked(Qt::Key keycode, Qt::KeyboardModifiers modifiers, const QHotkey::NativeShortcut &nativeShortcut);
|
Q_INVOKABLE void addMappingInvoked(Qt::Key keycode, Qt::KeyboardModifiers modifiers, QHotkey::NativeShortcut nativeShortcut);
|
||||||
Q_INVOKABLE bool addShortcutInvoked(QHotkey *hotkey);
|
Q_INVOKABLE bool addShortcutInvoked(QHotkey *hotkey);
|
||||||
Q_INVOKABLE bool removeShortcutInvoked(QHotkey *hotkey);
|
Q_INVOKABLE bool removeShortcutInvoked(QHotkey *hotkey);
|
||||||
Q_INVOKABLE QHotkey::NativeShortcut nativeShortcutInvoked(Qt::Key keycode, Qt::KeyboardModifiers modifiers);
|
Q_INVOKABLE QHotkey::NativeShortcut nativeShortcutInvoked(Qt::Key keycode, Qt::KeyboardModifiers modifiers);
|
||||||
@@ -51,7 +59,4 @@ private:
|
|||||||
return hotkeyPrivate;\
|
return hotkeyPrivate;\
|
||||||
}
|
}
|
||||||
|
|
||||||
Q_DECLARE_METATYPE(Qt::Key)
|
|
||||||
Q_DECLARE_METATYPE(Qt::KeyboardModifiers)
|
|
||||||
|
|
||||||
#endif // QHOTKEY_P_H
|
#endif // QHOTKEY_P_H
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ class QHotkeyPrivateWin : public QHotkeyPrivate
|
|||||||
public:
|
public:
|
||||||
QHotkeyPrivateWin();
|
QHotkeyPrivateWin();
|
||||||
// QAbstractNativeEventFilter interface
|
// QAbstractNativeEventFilter interface
|
||||||
bool nativeEventFilter(const QByteArray &eventType, void *message, long *result) Q_DECL_OVERRIDE;
|
bool nativeEventFilter(const QByteArray &eventType, void *message, _NATIVE_EVENT_RESULT *result) override;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void pollForHotkeyRelease();
|
void pollForHotkeyRelease();
|
||||||
@@ -42,7 +42,7 @@ bool QHotkeyPrivate::isPlatformSupported()
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool QHotkeyPrivateWin::nativeEventFilter(const QByteArray &eventType, void *message, long *result)
|
bool QHotkeyPrivateWin::nativeEventFilter(const QByteArray &eventType, void *message, _NATIVE_EVENT_RESULT *result)
|
||||||
{
|
{
|
||||||
Q_UNUSED(eventType)
|
Q_UNUSED(eventType)
|
||||||
Q_UNUSED(result)
|
Q_UNUSED(result)
|
||||||
@@ -71,7 +71,7 @@ quint32 QHotkeyPrivateWin::nativeKeycode(Qt::Key keycode, bool &ok)
|
|||||||
{
|
{
|
||||||
ok = true;
|
ok = true;
|
||||||
if(keycode <= 0xFFFF) {//Try to obtain the key from it's "character"
|
if(keycode <= 0xFFFF) {//Try to obtain the key from it's "character"
|
||||||
const SHORT vKey = VkKeyScanW(keycode);
|
const SHORT vKey = VkKeyScanW(static_cast<WCHAR>(keycode));
|
||||||
if(vKey > -1)
|
if(vKey > -1)
|
||||||
return LOBYTE(vKey);
|
return LOBYTE(vKey);
|
||||||
}
|
}
|
||||||
@@ -267,8 +267,7 @@ bool QHotkeyPrivateWin::registerShortcut(QHotkey::NativeShortcut shortcut)
|
|||||||
if(ok)
|
if(ok)
|
||||||
return true;
|
return true;
|
||||||
else {
|
else {
|
||||||
qCWarning(logQHotkey) << "Failed to register hotkey. Error:"
|
error = QHotkeyPrivateWin::formatWinError(::GetLastError());
|
||||||
<< qPrintable(QHotkeyPrivateWin::formatWinError(::GetLastError()));
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -279,8 +278,7 @@ bool QHotkeyPrivateWin::unregisterShortcut(QHotkey::NativeShortcut shortcut)
|
|||||||
if(ok)
|
if(ok)
|
||||||
return true;
|
return true;
|
||||||
else {
|
else {
|
||||||
qCWarning(logQHotkey) << "Failed to unregister hotkey. Error:"
|
error = QHotkeyPrivateWin::formatWinError(::GetLastError());
|
||||||
<< qPrintable(QHotkeyPrivateWin::formatWinError(::GetLastError()));
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+52
-31
@@ -1,7 +1,13 @@
|
|||||||
#include "qhotkey.h"
|
#include "qhotkey.h"
|
||||||
#include "qhotkey_p.h"
|
#include "qhotkey_p.h"
|
||||||
#include <QDebug>
|
|
||||||
#include <QX11Info>
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 2, 0)
|
||||||
|
#include <QGuiApplication>
|
||||||
|
#else
|
||||||
|
#include <QDebug>
|
||||||
|
#include <QX11Info>
|
||||||
|
#endif
|
||||||
|
|
||||||
#include <QThreadStorage>
|
#include <QThreadStorage>
|
||||||
#include <QTimer>
|
#include <QTimer>
|
||||||
#include <X11/Xlib.h>
|
#include <X11/Xlib.h>
|
||||||
@@ -16,20 +22,19 @@ class QHotkeyPrivateX11 : public QHotkeyPrivate
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
// QAbstractNativeEventFilter interface
|
// QAbstractNativeEventFilter interface
|
||||||
bool nativeEventFilter(const QByteArray &eventType, void *message, long *result) Q_DECL_OVERRIDE;
|
bool nativeEventFilter(const QByteArray &eventType, void *message, _NATIVE_EVENT_RESULT *result) override;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
// QHotkeyPrivate interface
|
// QHotkeyPrivate interface
|
||||||
quint32 nativeKeycode(Qt::Key keycode, bool &ok) Q_DECL_OVERRIDE;
|
quint32 nativeKeycode(Qt::Key keycode, bool &ok) Q_DECL_OVERRIDE;
|
||||||
quint32 nativeModifiers(Qt::KeyboardModifiers modifiers, bool &ok) Q_DECL_OVERRIDE;
|
quint32 nativeModifiers(Qt::KeyboardModifiers modifiers, bool &ok) Q_DECL_OVERRIDE;
|
||||||
QString getX11String(Qt::Key keycode);
|
static QString getX11String(Qt::Key keycode);
|
||||||
bool registerShortcut(QHotkey::NativeShortcut shortcut) Q_DECL_OVERRIDE;
|
bool registerShortcut(QHotkey::NativeShortcut shortcut) Q_DECL_OVERRIDE;
|
||||||
bool unregisterShortcut(QHotkey::NativeShortcut shortcut) Q_DECL_OVERRIDE;
|
bool unregisterShortcut(QHotkey::NativeShortcut shortcut) Q_DECL_OVERRIDE;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static const QVector<quint32> specialModifiers;
|
static const QVector<quint32> specialModifiers;
|
||||||
static const quint32 validModsMask;
|
static const quint32 validModsMask;
|
||||||
QTimer *releaseTimer = nullptr;
|
|
||||||
xcb_key_press_event_t prevHandledEvent;
|
xcb_key_press_event_t prevHandledEvent;
|
||||||
xcb_key_press_event_t prevEvent;
|
xcb_key_press_event_t prevEvent;
|
||||||
|
|
||||||
@@ -53,18 +58,22 @@ NATIVE_INSTANCE(QHotkeyPrivateX11)
|
|||||||
|
|
||||||
bool QHotkeyPrivate::isPlatformSupported()
|
bool QHotkeyPrivate::isPlatformSupported()
|
||||||
{
|
{
|
||||||
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 2, 0)
|
||||||
|
return qGuiApp->nativeInterface<QNativeInterface::QX11Application>();
|
||||||
|
#else
|
||||||
return QX11Info::isPlatformX11();
|
return QX11Info::isPlatformX11();
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
const QVector<quint32> QHotkeyPrivateX11::specialModifiers = {0, Mod2Mask, LockMask, (Mod2Mask | LockMask)};
|
const QVector<quint32> QHotkeyPrivateX11::specialModifiers = {0, Mod2Mask, LockMask, (Mod2Mask | LockMask)};
|
||||||
const quint32 QHotkeyPrivateX11::validModsMask = ShiftMask | ControlMask | Mod1Mask | Mod4Mask;
|
const quint32 QHotkeyPrivateX11::validModsMask = ShiftMask | ControlMask | Mod1Mask | Mod4Mask;
|
||||||
|
|
||||||
bool QHotkeyPrivateX11::nativeEventFilter(const QByteArray &eventType, void *message, long *result)
|
bool QHotkeyPrivateX11::nativeEventFilter(const QByteArray &eventType, void *message, _NATIVE_EVENT_RESULT *result)
|
||||||
{
|
{
|
||||||
Q_UNUSED(eventType)
|
Q_UNUSED(eventType)
|
||||||
Q_UNUSED(result)
|
Q_UNUSED(result)
|
||||||
|
|
||||||
xcb_generic_event_t *genericEvent = static_cast<xcb_generic_event_t *>(message);
|
auto *genericEvent = static_cast<xcb_generic_event_t *>(message);
|
||||||
if (genericEvent->response_type == XCB_KEY_PRESS) {
|
if (genericEvent->response_type == XCB_KEY_PRESS) {
|
||||||
xcb_key_press_event_t keyEvent = *static_cast<xcb_key_press_event_t *>(message);
|
xcb_key_press_event_t keyEvent = *static_cast<xcb_key_press_event_t *>(message);
|
||||||
this->prevEvent = keyEvent;
|
this->prevEvent = keyEvent;
|
||||||
@@ -75,17 +84,11 @@ bool QHotkeyPrivateX11::nativeEventFilter(const QByteArray &eventType, void *mes
|
|||||||
} else if (genericEvent->response_type == XCB_KEY_RELEASE) {
|
} else if (genericEvent->response_type == XCB_KEY_RELEASE) {
|
||||||
xcb_key_release_event_t keyEvent = *static_cast<xcb_key_release_event_t *>(message);
|
xcb_key_release_event_t keyEvent = *static_cast<xcb_key_release_event_t *>(message);
|
||||||
this->prevEvent = keyEvent;
|
this->prevEvent = keyEvent;
|
||||||
QTimer *timer = new QTimer(this);
|
QTimer::singleShot(50, [this, keyEvent] {
|
||||||
timer->setSingleShot(true);
|
|
||||||
timer->setInterval(50);
|
|
||||||
connect(timer, &QTimer::timeout, this, [this, keyEvent, timer] {
|
|
||||||
if(this->prevEvent.time == keyEvent.time && this->prevEvent.response_type == keyEvent.response_type && this->prevEvent.detail == keyEvent.detail){
|
if(this->prevEvent.time == keyEvent.time && this->prevEvent.response_type == keyEvent.response_type && this->prevEvent.detail == keyEvent.detail){
|
||||||
this->releaseShortcut({keyEvent.detail, keyEvent.state & QHotkeyPrivateX11::validModsMask});
|
this->releaseShortcut({keyEvent.detail, keyEvent.state & QHotkeyPrivateX11::validModsMask});
|
||||||
}
|
}
|
||||||
delete timer;
|
|
||||||
});
|
});
|
||||||
timer->start();
|
|
||||||
this->releaseTimer = timer;
|
|
||||||
this->prevHandledEvent = keyEvent;
|
this->prevHandledEvent = keyEvent;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -98,17 +101,17 @@ QString QHotkeyPrivateX11::getX11String(Qt::Key keycode)
|
|||||||
|
|
||||||
case Qt::Key_MediaLast :
|
case Qt::Key_MediaLast :
|
||||||
case Qt::Key_MediaPrevious :
|
case Qt::Key_MediaPrevious :
|
||||||
return "XF86AudioPrev";
|
return QStringLiteral("XF86AudioPrev");
|
||||||
case Qt::Key_MediaNext :
|
case Qt::Key_MediaNext :
|
||||||
return "XF86AudioNext";
|
return QStringLiteral("XF86AudioNext");
|
||||||
case Qt::Key_MediaPause :
|
case Qt::Key_MediaPause :
|
||||||
case Qt::Key_MediaPlay :
|
case Qt::Key_MediaPlay :
|
||||||
case Qt::Key_MediaTogglePlayPause :
|
case Qt::Key_MediaTogglePlayPause :
|
||||||
return "XF86AudioPlay";
|
return QStringLiteral("XF86AudioPlay");
|
||||||
case Qt::Key_MediaRecord :
|
case Qt::Key_MediaRecord :
|
||||||
return "XF86AudioRecord";
|
return QStringLiteral("XF86AudioRecord");
|
||||||
case Qt::Key_MediaStop :
|
case Qt::Key_MediaStop :
|
||||||
return "XF86AudioStop";
|
return QStringLiteral("XF86AudioStop");
|
||||||
default :
|
default :
|
||||||
return QKeySequence(keycode).toString(QKeySequence::NativeText);
|
return QKeySequence(keycode).toString(QKeySequence::NativeText);
|
||||||
}
|
}
|
||||||
@@ -127,12 +130,20 @@ quint32 QHotkeyPrivateX11::nativeKeycode(Qt::Key keycode, bool &ok)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(QX11Info::isPlatformX11()) {
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 2, 0)
|
||||||
auto res = XKeysymToKeycode(QX11Info::display(), keysym);
|
const QNativeInterface::QX11Application *x11Interface = qGuiApp->nativeInterface<QNativeInterface::QX11Application>();
|
||||||
|
Display *display = x11Interface->display();
|
||||||
|
#else
|
||||||
|
const bool x11Interface = QX11Info::isPlatformX11();
|
||||||
|
Display *display = QX11Info::display();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if(x11Interface) {
|
||||||
|
auto res = XKeysymToKeycode(display, keysym);
|
||||||
if(res != 0)
|
if(res != 0)
|
||||||
ok = true;
|
ok = true;
|
||||||
return res;
|
return res;
|
||||||
} else
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -153,8 +164,15 @@ quint32 QHotkeyPrivateX11::nativeModifiers(Qt::KeyboardModifiers modifiers, bool
|
|||||||
|
|
||||||
bool QHotkeyPrivateX11::registerShortcut(QHotkey::NativeShortcut shortcut)
|
bool QHotkeyPrivateX11::registerShortcut(QHotkey::NativeShortcut shortcut)
|
||||||
{
|
{
|
||||||
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 2, 0)
|
||||||
|
const QNativeInterface::QX11Application *x11Interface = qGuiApp->nativeInterface<QNativeInterface::QX11Application>();
|
||||||
|
Display *display = x11Interface->display();
|
||||||
|
#else
|
||||||
|
const bool x11Interface = QX11Info::isPlatformX11();
|
||||||
Display *display = QX11Info::display();
|
Display *display = QX11Info::display();
|
||||||
if(!display)
|
#endif
|
||||||
|
|
||||||
|
if(!display || !x11Interface)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
HotkeyErrorHandler errorHandler;
|
HotkeyErrorHandler errorHandler;
|
||||||
@@ -170,17 +188,21 @@ bool QHotkeyPrivateX11::registerShortcut(QHotkey::NativeShortcut shortcut)
|
|||||||
XSync(display, False);
|
XSync(display, False);
|
||||||
|
|
||||||
if(errorHandler.hasError) {
|
if(errorHandler.hasError) {
|
||||||
qCWarning(logQHotkey) << "Failed to register hotkey. Error:"
|
error = errorHandler.errorString;
|
||||||
<< qPrintable(errorHandler.errorString);
|
|
||||||
this->unregisterShortcut(shortcut);
|
this->unregisterShortcut(shortcut);
|
||||||
return false;
|
return false;
|
||||||
} else
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool QHotkeyPrivateX11::unregisterShortcut(QHotkey::NativeShortcut shortcut)
|
bool QHotkeyPrivateX11::unregisterShortcut(QHotkey::NativeShortcut shortcut)
|
||||||
{
|
{
|
||||||
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 2, 0)
|
||||||
|
Display *display = qGuiApp->nativeInterface<QNativeInterface::QX11Application>()->display();
|
||||||
|
#else
|
||||||
Display *display = QX11Info::display();
|
Display *display = QX11Info::display();
|
||||||
|
#endif
|
||||||
|
|
||||||
if(!display)
|
if(!display)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
@@ -189,15 +211,14 @@ bool QHotkeyPrivateX11::unregisterShortcut(QHotkey::NativeShortcut shortcut)
|
|||||||
XUngrabKey(display,
|
XUngrabKey(display,
|
||||||
shortcut.key,
|
shortcut.key,
|
||||||
shortcut.modifier | specialMod,
|
shortcut.modifier | specialMod,
|
||||||
DefaultRootWindow(display));
|
XDefaultRootWindow(display));
|
||||||
}
|
}
|
||||||
XSync(display, False);
|
XSync(display, False);
|
||||||
|
|
||||||
if(errorHandler.hasError) {
|
if(HotkeyErrorHandler::hasError) {
|
||||||
qCWarning(logQHotkey) << "Failed to unregister hotkey. Error:"
|
error = HotkeyErrorHandler::errorString;
|
||||||
<< qPrintable(errorHandler.errorString);
|
|
||||||
return false;
|
return false;
|
||||||
} else
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -14,6 +14,32 @@ The QHotkey is a class that can be used to create hotkeys/global shortcuts, aka
|
|||||||
|
|
||||||
**Note:** For now Wayland is not supported, as it is simply not possible to register a global shortcut with wayland. For more details, or possible Ideas on how to get Hotkeys working on wayland, see [Issue #14](https://github.com/Skycoder42/QHotkey/issues/14).
|
**Note:** For now Wayland is not supported, as it is simply not possible to register a global shortcut with wayland. For more details, or possible Ideas on how to get Hotkeys working on wayland, see [Issue #14](https://github.com/Skycoder42/QHotkey/issues/14).
|
||||||
|
|
||||||
|
## Building
|
||||||
|
|
||||||
|
QHotkey supports both Qt6 and Qt5. When using Qt6, version 6.2.0 or later required. It can be built by either the CMake or qmake building systems.
|
||||||
|
|
||||||
|
### CMake
|
||||||
|
|
||||||
|
The CMake `QT_MAJOR` variable controls which major version of Qt is used for building, and defaults to `5`. For example, use the CMake command line option `-DQT_MAJOR=6` for building with Qt6. To build the testing application `QHotkeyTest`, specify `-DQHOTKEY_EXAMPLES=ON`. CMake example usage:
|
||||||
|
|
||||||
|
```
|
||||||
|
$ cd QHotkey
|
||||||
|
$ cmake -B build -S . -DQT_MAJOR=6
|
||||||
|
$ cmake --build build
|
||||||
|
# cmake --install build
|
||||||
|
```
|
||||||
|
|
||||||
|
### qmake
|
||||||
|
|
||||||
|
The major version of Qt is chosen by the qmake invokation itself, as the qmake executable is tied to a specific Qt version. The executable name can vary between operating systems. qmake example usage:
|
||||||
|
|
||||||
|
```
|
||||||
|
$ cd QHotkey
|
||||||
|
$ qmake
|
||||||
|
$ make
|
||||||
|
# make install
|
||||||
|
```
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
The package is providet as qpm package, [`de.skycoder42.qhotkey`](https://www.qpm.io/packages/de.skycoder42.qhotkey/index.html). You can install it either via qpmx (preferred) or directly via qpm.
|
The package is providet as qpm package, [`de.skycoder42.qhotkey`](https://www.qpm.io/packages/de.skycoder42.qhotkey/index.html). You can install it either via qpmx (preferred) or directly via qpm.
|
||||||
|
|
||||||
@@ -48,17 +74,17 @@ The following example shows a simple application that will run without a window
|
|||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
QApplication a(argc, argv);
|
QApplication app(argc, argv);
|
||||||
|
|
||||||
auto hotkey = new QHotkey(QKeySequence("ctrl+alt+Q"), true, &a);//The hotkey will be automatically registered
|
QHotkey hotkey(QKeySequence("Ctrl+Alt+Q"), true, &app); //The hotkey will be automatically registered
|
||||||
qDebug() << "Is Registered: " << hotkey->isRegistered();
|
qDebug() << "Is segistered:" << hotkey.isRegistered();
|
||||||
|
|
||||||
QObject::connect(hotkey, &QHotkey::activated, qApp, [&](){
|
QObject::connect(&hotkey, &QHotkey::activated, qApp, [&](){
|
||||||
qDebug() << "Hotkey Activated - the application will quit now";
|
qDebug() << "Hotkey Activated - the application will quit now";
|
||||||
qApp->quit();
|
qApp->quit();
|
||||||
});
|
});
|
||||||
|
|
||||||
return a.exec();
|
return app.exec();
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,15 @@
|
|||||||
mac: LIBS += -framework Carbon
|
mac: LIBS += -framework Carbon
|
||||||
else:win32: LIBS += -luser32
|
else:win32: LIBS += -luser32
|
||||||
else:unix {
|
else:unix {
|
||||||
|
equals(QT_MAJOR_VERSION, 6) {
|
||||||
|
lessThan(QT_VERSION, 6.2.0) {
|
||||||
|
error("Qt 6.2.0 or greater is required when using Qt6")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
lessThan(QT_MAJOR_VERSION, 6) {
|
||||||
QT += x11extras
|
QT += x11extras
|
||||||
|
}
|
||||||
|
}
|
||||||
LIBS += -lX11
|
LIBS += -lX11
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user